NULL definition

Schwarz, Konrad
Mon Nov 5 19:12:00 GMT 2012

> -----Original Message-----
> From: []
> 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.

If I remember correctly, in C++, NULL must expand to literal 0.  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).
> 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?

Konrad Schwarz

More information about the Newlib mailing list