Definition
A null pointer is a pointer value that points to no object and no function. C guarantees that it compares unequal to every pointer to an object or function, and equal to every other null pointer. A program writes one as a null pointer constant: the integer constant 0, the macro NULL, or, since C23, the edition of the C standard published in 2024 [309], nullptr. Functions return a null pointer to mean “no object”, as malloc does when it cannot allocate memory. Reading or writing through a null pointer is undefined behavior §4.12.
The idea
A pointer variable often needs a value that means “nothing here”: the end of a linked list §14.6, an optional argument left out, a failed allocation, a slot that holds no record yet. Without a reserved value, every such pointer would need a separate flag beside it. The null pointer is that reserved value. Because it differs from the address of every object, the single comparison p == NULL separates “no object” from “some object”.
A null pointer marks absence and says nothing more. An envelope is a fair picture: an empty address field shows at a glance that there is no recipient, but a filled-in address may still lead to a demolished house or the wrong street, and a check for an empty field detects neither. The picture stops being accurate in one respect: the post office returns an undeliverable letter, while a C program that writes through a pointer to freed memory gets no error at all. Tony Hoare, who added null references to a programming language in 1965, later called them his “billion-dollar mistake” [458], because so many programs use a pointer without first checking it for null.
How it works
Writing a null pointer. Section 6.3.2.3 of C11, the 2011 edition of the C standard, defines a null pointer constant as an integer constant expression with the value 0, or such an expression converted to
void *. The macroNULL, defined in<stddef.h>and several other headers, expands to one; C23, the edition of the standard published in 2024, addsnullptr, a constant that always has pointer meaning. Converting a null pointer constant to any pointer type gives that type’s null pointer.Default values. A pointer with static storage duration §4.11, such as a global variable, starts as a null pointer when it has no initializer. A local pointer variable without an initializer does not: its value is indeterminate.
Testing.
if (p != NULL)andif (p)are the same test.Representation. The standard does not require a null pointer to be stored as all-zero bits. On today’s mainstream processors it is address 0.
Dereferencing at run time. Undefined behavior means that the standard does not say what happens, and machines differ. Operating systems with virtual memory, run by a memory management unit §6.9 (MMU), leave the lowest addresses unmapped, so a null dereference stops the program with a fault, on Linux a segmentation fault. On a microcontroller §6.1, address 0 is often ordinary memory. On many Arm Cortex-M chips it holds the vector table §6.14, the list of start addresses of the exception handlers §6.15, so a read through a null pointer silently returns those values. A memory protection unit §6.8 (MPU) can be set to forbid access to a small region at address 0, which turns such reads into faults.
Library functions have their own contracts. free(NULL) does nothing, by definition. malloc(0) returns either a null pointer or a unique pointer that must not be dereferenced; which one is implementation-defined behavior §4.13, so a null result from malloc(0) is not necessarily a failure. Passing a null pointer to a function that expects a valid pointer, such as memcpy, is undefined behavior in C11 even when the length is 0.
Worked example
A non-null pointer that is no longer valid. The addresses are invented.
An owner
Ocallsp = malloc(16), receives0x20001000, checks that it is not null, and stores a record there.Ogives a readerRa copy:r = p.Ofinishes with the record, callsfree(p)and setsp = NULL.Another part of the program calls
malloc(16)and receives0x20001000again.Rchecksr != NULL, which is true, and writes throughr. The write lands in the new, unrelated object.
The check in step 5 proved only that r is not the null pointer. r is a dangling pointer §4.1, because its object’s lifetime ended in step 3, and the write is a use-after-free §4.6. Setting p to null in step 3 changed p, not r. Even before step 3, a non-null pointer can fail on bounds: a 4-byte read that starts at byte 14 of the 16-byte record covers bytes 14 to 17, and bytes 16 and 17 lie past the record’s last byte, byte 15.
A check that the compiler deletes. Consider
int n = hdr->len;
if (hdr == NULL) return -1;
The first line dereferences hdr, and dereferencing a null pointer is undefined, so an optimizing compiler may conclude that hdr is not null and delete the check. In 2009 this pattern in a network driver of the Linux kernel, combined with an attacker who placed data at address 0, gave a privilege-escalation exploit, recorded as CVE-2009-1897 (CVE means Common Vulnerabilities and Exposures, the public catalog of known vulnerabilities). The kernel has since been compiled with -fno-delete-null-pointer-checks, an option of GCC §12.17, the GNU Compiler Collection (GNU is a free-software project whose name stands for GNU’s Not Unix), which keeps such checks.
Stated precisely
C11 section 6.3.2.3 states the contract:
A null pointer constant is an integer constant expression with the value 0, or such an expression cast to
void *.Converting a null pointer constant to a pointer type gives a null pointer, which is guaranteed to compare unequal to a pointer to any object or function.
Converting a null pointer to another pointer type gives a null pointer of that type, and any two null pointers compare equal.
Section 6.5.3.2 makes applying the dereference operator * to a null pointer undefined behavior, and a member access through -> is a dereference too.
The guarantee is a single inequality. That a pointer compares unequal to the null pointer means only that is not the null pointer. It implies nothing about the lifetime of the object was made for, about bounds, about alignment §4.22, or about the type of the bytes at that address.
Limits and common mistakes
Not null is not valid. A dangling pointer, a pointer past the end of an object, a misaligned pointer and an uninitialized pointer are usually all non-null.
Nulling one copy does not null the others. Setting a pointer to null after free protects that one variable: a later free of it does nothing, and a later dereference faults where address 0 is unmapped. Every other copy still dangles.
Check before use, not after. A check placed after a dereference may be deleted by the compiler, as the example shows.
Address 0 may be real memory. On a microcontroller whose memory protection unit does not guard address 0, a null dereference reads data and may go unnoticed, so a test that waits for a crash misses it.
All-zero bits are not guaranteed to be null. memset with 0 and calloc produce null pointers only on machines whose null pointer is stored as zero bits: every mainstream machine, but not every machine the C standard allows.
NULL as an argument of a variadic function. A variadic function, such as execl, takes a variable number of arguments of unchecked types. Where NULL expands to the plain integer 0 and pointers are wider than int, passing NULL to it passes an integer instead of a pointer; write (char *)NULL.
Null pointer arithmetic §4.25. Adding even 0 to a null pointer, or passing a null pointer with a length of 0 to memcpy, is undefined in C11.
Questions answered
Can a successful allocation still become invalid through another owner?
Yes. A successful allocation returns a non-null pointer to an object that is alive at that moment, but validity belongs to the object, not to the pointer value. Another part of the program that holds a copy of the pointer can later end the object’s lifetime, for example by calling free, or by calling realloc, which may move the data and release the old block. From then on every remaining copy is a dangling pointer: it still holds the same non-null address, so a comparison with the null pointer, the special value that points to no object, still passes, yet any use of it is a use-after-free.
Setting the freeing owner’s own variable to null does not change the copies held elsewhere. The protection lies in explicit rules of ownership §4.2: one owner frees the object, and only after every borrower has finished with it; or the object keeps a reference count, a count of its current users, and is freed when the count reaches zero; or each holder keeps a handle with a generation number that is checked before every use.
Related entries: Object lifetime (§4.1), Borrowed pointer (§4.3), Use-after-free (§4.6), Ownership (§4.2), Memory protection unit (MPU) (§6.8), Pointer arithmetic (§4.25). Further reading: [78, 309, 458].