This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: NULL definition


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   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]