null pointer

From Cppwiki

Jump to: navigation, search

A null pointer is a pointer whose value is equal to the null pointer constant. A null pointer does not point to any object; attempting to dereference a null pointer is undefined behaviour. A null pointer will never compare equal to any pointer-to-object; it can be tested for by comparison with 0:

if (p == 0) {
  /* p is a null pointer */
  ...

Null pointer constant

A null pointer constant is any integral expression which evaluates to 0. Examples of null pointer constants are 0 and 0L. (void *)0 is not a null pointer constant, because it does not have integral type.

NULL

The NULL macro (defined in <cstdlib> and several other headers) expands to an implementation-defined null pointer constant. In C89, NULL was (void *)0; in C++, this is forbidden.

__null

__null is a GCC-specific keyword which evaluates to an integral 0 constant. __null is the usual expansion of NULL on gcc. __null behaves identically to 0, except that when used in a variadic parameter list, its type is the same width as void*. This prevents problems with null-terminated variadic functions like execl(); on a platform where sizeof(int) < sizeof(void *), terminating the list with NULL (i.e. 0) may cause a shorter value to be pushed onto the stack than the function expects, and it will not be treated as the end of the argument list (probably crashing the program).

Personal tools