C++ Notes: The NULL Pointer

Problem: Initial value for pointers

Variables are not automatically given an intial value by the system, and start with whatever garbage is left in memory when they are allocated. Pointers have the potential to cause substantial damage when they are used without a valid address. For this reason it's important to initialize them.

NULL. The standard initialization is to the predefined constant NULL. Using the NULL value as a pointer will cause an error on almost all systems, making the debugging process somewhat easier. NULL is defined in <cstdlib>.

#include <cstdlib>
. . .
Node* head = NULL;   // Initialized pointer to NULL.

Is NULL zero?

In theory, NULL can be defined to be non-zero. In practice this doesn't happen on any mainstream system. So much code is written making the assumption that NULL is zero, it is hard to image it using anything except 0.