SHORT Re: Suggestion: "fix" assert(obj)

Georg Wrede georg at nospam.org
Sat Jun 16 04:43:31 PDT 2007


Walter Bright wrote:
> Georg Wrede wrote:
>> Walter Bright wrote:
>>> Kristian Kilpi wrote:
>>>> The problem is that assert(obj); does not first check if 'obj' is null.
>>>
>>> Yes it does, it's just that the hardware does the check, and gives 
>>> you a  seg fault exception if it is null.
>>
>> Asserts were INVENTED to *avoid segfaults*.
> 
> The x86 had no hardware protection. When you wrote through a NULL pointer, 
> you scrambled the operating systems, and all kinds of terrible, 
> unpredictable things ensued. Asserts were used a lot to try and head off 
> these problems.

Ok, sloppy wording on my part. "Pointer disasters" would've been more 
accurate.

> Seg faults are not an evil thing, they're there to help you. In fact, 
> I'll often *deliberately* code them in so the debugger will pop up when 
> it hits them.

That's okay. But what you do with D is hardly the average programming 
task. And not everybody uses a debugger. Many have their code run by 
others, and a phone call about a segfault is not an option. This is 
where asserts come in handy.

Sure, we can learn to write if(o) and assert(o !is null). Maybe this 
discussion should be deferred until nothing big is going on in D.



More information about the Digitalmars-d mailing list