This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
RE: NULL definition
- From: "Schwarz, Konrad" <konrad dot schwarz at siemens dot com>
- To: "newlib at sourceware dot org" <newlib at sourceware dot org>
- Date: Wed, 31 Oct 2012 17:35:31 +0100
- Subject: RE: NULL definition
- References: <20121031160524.GF21252@calimero.vinschen.de>
> -----Original Message-----
> 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.
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