A null pointer in C++ is a pointer that does not point to any object or function. Since C++11 you create one with the keyword nullptr, and you can compare any pointer against it to find out whether it refers to something. Dereferencing a null pointer is undefined behavior, so you must check or avoid it before using *ptr or ptr->member.
Null pointers are a normal part of C++ APIs: they signal "no result", "not set yet" or "no more nodes". Modern C++ also gives you safer tools, such as references, std::optional and smart pointers, for cases where a raw pointer is not the best fit.
Every pointer type has a special null pointer value. It compares unequal to the address of any valid object and unequal to the address of any function. A pointer holding that value says "I currently refer to nothing".
The null pointer value is not guaranteed to be "address zero" at the hardware level, although it is on mainstream platforms. Always write nullptr and let the compiler pick the representation.
nullptrnullptr is a keyword introduced in C++11. Its type is std::nullptr_t, which converts implicitly to every pointer type and to bool, but not to an integer type. That prevents the overload and template mistakes that the older spellings allow.
| Spelling | Status | Problem |
|---|---|---|
nullptr | Recommended since C++11 | None, it has its own type |
NULL | Legacy macro | Expands to an integer constant such as 0 or 0L, so it can select an int overload |
0 | Legacy | A plain integer that happens to convert to a pointer |
A pointer converts to bool in a condition: null becomes false, anything else becomes true. These checks are equivalent:
Pick one style and use it consistently across a code base.
An uninitialised pointer contains an indeterminate value. Reading it is undefined behavior, and it may look like a valid address. Initialising to nullptr turns a random failure into a failure you can check.
Functions that search often return a pointer and use nullptr for a miss:
The pointer returned here is non-owning and is only valid while the vector is not resized. Do not keep it around after calling push_back.
Linked lists, trees and graphs commonly use nullptr for "no next node" or "no child".
Reading or writing through a null pointer is undefined behavior. On most desktop and server systems the program crashes with a segmentation fault or an access violation, but the standard does not promise a crash. The optimiser may also assume a pointer is never null once you have dereferenced it, and remove later null checks.
Two related rules are worth knowing:
delete nullptr; and delete p; where p is null are safe and do nothing, so you do not need a check before delete.Raw pointers are the right tool when "may be absent" and "does not own the object" are both true. In other situations prefer one of these:
| Need | Prefer |
|---|---|
| A parameter that must always refer to an object | A reference, T& or const T& |
| A value that may be absent, stored by value | std::optional<T> |
| Exclusive ownership of a heap object | std::unique_ptr<T> |
| Shared ownership | std::shared_ptr<T> |
| A non-owning view of a sequence | std::span<T> (C++20) or std::string_view |
Smart pointers can themselves be empty, and they compare with nullptr:
Create them with std::make_unique and std::make_shared rather than calling new directly.
No. A null pointer is a distinct pointer value, and the integer zero only converts to it in legacy code. In new code write nullptr, which has type std::nullptr_t and cannot be mistaken for a number.
NULL in C++?No. NULL is a macro inherited from C and can pick the wrong overload because it expands to an integer constant. Use nullptr in every C++11 or later code base.
A null pointer is deliberately set to "nothing" and can be tested safely. A dangling pointer still holds an address, but the object there has been destroyed, and nothing in the pointer tells you so. Using smart pointers and clear ownership rules avoids dangling pointers.
delete on a null pointer crash?No. Deleting a null pointer is defined to do nothing, so it is safe to call without a check.
A null pointer refers to no object, and in C++ you write it as nullptr. Initialise pointers you cannot assign yet, test them before dereferencing, and never rely on a crash to report a mistake. Prefer references, std::optional and smart pointers when they express your intent better than a raw pointer.