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 Oct 31 17:35, Schwarz, Konrad wrote:
> > From: newlib-owner@sourceware.org [mailto:newlib-owner@sourceware.org]
> > Subject: NULL definition
> > 
> 
> > Even if you ignore the optional part, the important snippet here is
> > that NULL expands to a Null *pointer* value.  However, our definition
> > of NULL is just 0, so it is of type int.  But on many targets
> > sizeof(int) is != sizeof(void*).  This could potentially result in size
> > problems.
> 
> This is not really correct. C defines a "null pointer constant" as any
> integral constant expression with value 0 or such an expression cast to
> void *.
> 
> Assigning a null pointer constant to a pointer or comparing a null pointer
> constant for equality with a pointer causes the null pointer constant
> to assume the proper pointer type.
> 
> Literal 0 is a trivial example of a null pointer constant.

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*).

> 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).

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.


Corinna

-- 
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat


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