NULL definition

Eric Blake
Tue Nov 6 13:32:00 GMT 2012

On 10/31/2012 10:53 AM, Corinna Vinschen wrote:
> Literal 0 may get you into trouble in case of varargs:
>   printf ("%p %s\n", NULL, "is the NULL pointer value");
> There's *no* guarantee that this prints "0 is the NULL pointer value"
> if sizeof (int) != sizeof (void*).

Yep, which is why POSIX requires NULL to expand to ((void*)0) in C.

>> If I remember correctly, in C++, NULL must expand to literal 0.
> This is what stddef.h will do.
>> In C89,
>> NULL expands to an implementation-specific null-pointer constant (could be
>> a literal 0).  In C99, NULL must expand to a ((void *) 0).

Not quite accurate - C99 doesn't require this; it is POSIX that adds the
additional restriction (which is why it was listed under the <CX> option
in POSIX - it is an extension requirement not in C but mandated in
POSIX).  But the point remains:

> This is what stddef.h will do.
>>> So, given that we already rely on stddef.h anyway throughout our header
>>> files, I wonder if we shouldn't change the definition of NULL by
>>> including stddef.h as well, so we always get it right:
>>>   #define __need_NULL
>>>   #include <stddef.h>
>> Won't this introduce additional definitions (offsetof, size_t, ptrdiff_t, ...)
>> that not all headers defining NULL are specified to define?
> Not at all.  That's what the `#define __need_NULL' is about.

This proposal of always using <stddef.h> looks correct to me.

Eric Blake    +1-919-301-3266
Libvirt virtualization library

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 617 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the Newlib mailing list