Subject: Re: NULL return value checking
From: David Laight <email@example.com>
Date: 04/24/2002 18:36:33
On Wed, Apr 24, 2002 at 11:20:40AM -0400, der Mouse wrote:
> Two things here. First, TLD writes
> > I read somewhere, NULL can be != 0
> > IMHO assuming NULL == 0 is bad practice if portability is a target.
> Others have said similar things.
> There are two ways in which it makes sense to say "NULL == 0": one is
> "null pointer is all-bits-zero", the other is "null pointer compares
> equal to zero in the language". [%]
> The former is not guaranteed by the language, but the latter is. I
> suspect this may be the source of some of the confusion in this thread.
> [%] Actually, there's a third, "NULL is #defined as 0" (as opposed to,
> say, (void *)0), but that's not interesting at the moment.
I don't use NULL, not ever! I use 'if (pointer)', 'if (!pointer)' and
'pointer = 0'.
All the above are completely portable, and always valid C.
OTOH someone, in their infinite wisdom, might have decided to define
NULL in some other way than '#define NULL 0' in which case any code
that references NULL might break......
(I also have a habit of assuming that the null pointer as the zero
bit pattern because I will use memset, bzero and calloc to set
pointers to null. There again I haven't written C for a system
that used a different bit pattern for its null pointer - only
Algol86 and S3)
David Laight: firstname.lastname@example.org