NULL definition

Corinna Vinschen
Thu Nov 1 15:56:00 GMT 2012


while working on something completely different, I stumbled over the
definition of NULL in newlib.

Throughout newlib, NULL is defined like this:

  #ifndef NULL
  #define NULL 0

The problem with this definition is that it's not quite wrong, but
also not really correct, according to POSIX.1-2008:

   The <stddef.h> header shall define the following macros:
      Null pointer constant. ^[CX] [Option Start]  The macro shall
      expand to an integer constant expression with the value 0 cast
      to type void *. [Option End]

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

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>

Any problems with that?


Corinna Vinschen
Cygwin Project Co-Leader
Red Hat

More information about the Newlib mailing list