Suggestion: "fix" assert(obj)

Stewart Gordon smjg_1998 at yahoo.com
Sun Jun 17 06:41:16 PDT 2007


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:f4s0i8$1vk8$1 at digitalmars.com...
> 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.

There are two problems with this.

Firstly, this'll only work if the class actually has an invariant, and this 
invariant actually accesses object members (as opposed to merely checking 
static stuff).

Secondly, given a segfault or AV, how's one supposed to pinpoint the cause? 
An assert error, OTOH, has a filename and line number attached.  It's the 
same with the more general request of null reference checking - people want 
an error class that unambiguously indicates dereferencing a null pointer and 
which gives an immediate clue of where the problem is.

Stewart. 




More information about the Digitalmars-d mailing list