NULL definition

Christopher Faylor
Fri Nov 2 16:10:00 GMT 2012

On Wed, Oct 31, 2012 at 05:05:24PM +0100, Corinna Vinschen wrote:
>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
>  #endif
>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
>      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?

Looks right to me.


More information about the Newlib mailing list