The short answer
Use nullptr in C++ code. NULL is an older macro, usually defined as the integer 0, and that can cause surprises with overloaded functions. nullptr was added in C++11. It has its own type, which avoids those surprises.
What NULL is
NULL is a macro from standard headers such as <cstddef>. The preprocessor replaces it with a value before the compiler runs. Many compilers define it as 0. Some use a built-in value instead, such as __null in GCC, which has an integer type.
Because NULL is a macro, the compiler sees only what it expands to. The exact definition can differ from one compiler or platform to another.
Readers new to the topic often first ask, what is a null pointer in C? A null pointer is a pointer that does not point to any object or function. It is the value you use to say that a pointer points nowhere.
What nullptr is
nullptr is a keyword, not a macro. Its type is std::nullptr_t, which is declared in <cstddef>. A nullptr converts implicitly to any pointer type. It does not convert to an integer type, so this line does not compile:
int x = nullptr; // Error: nullptr cannot initialize an int.
Where the difference shows: overloads
Overloaded functions are where the two forms behave differently:
#include <iostream>
void show(int value) {
std::cout << "called with int\n";
}
void show(char* text) {
std::cout << "called with pointer\n";
}
int main() {
show(0); // Calls show(int).
show(nullptr); // Calls show(char*).
}
A literal 0 is an int, so the first call picks the int overload. nullptr is not an integer, so the second call picks the pointer overload.
With NULL, the result depends on how your compiler defines it. Depending on the compiler, show(NULL) may call the int overload, or the call may be ambiguous and fail to compile.
Readers often ask, why use null instead of 0? In C++, the overload case above is the main practical answer, because a plain 0 is always an int.
Comparisons and assignments
Both forms work in ordinary comparisons:
int* p = nullptr;
if (p == nullptr) {
// p does not point to an object.
}
Code that compares against NULL also compiles on most compilers. The difference is that nullptr makes it clear the value is a pointer, which helps both readers and tools.
Migrating old code
Replacing NULL with nullptr is usually safe, but test the result. The clang-tidy check modernize-use-nullptr can find NULL and 0 used as pointers and suggest the change.
Be careful with C-style variadic functions such as printf and execl. Whether a null argument works there depends on the exact type it is passed as, so review those calls. A cast such as (char*)nullptr makes the pointer type explicit.
Deleting a nullptr
A common follow-up question is what happens when you delete a nullptr? In C++, delete on a null pointer does nothing and is safe, so you do not need an if check before it. Deleting the same non-null pointer twice is a different case, and it is undefined behavior.
NULL and nullptr in C
In C, NULL is still the common choice. C23 added a nullptr constant too, but older compilers and existing code do not have it, so check your compiler and language standard before using it in C code.
Readers coming from C often ask, is 0 == null in C? The answer is yes, because both values are null pointer constants. That does not make the two forms interchangeable for readers, which is why the choice still matters.
Common mistakes
- Assuming a new pointer is null. A local pointer you do not initialize holds an indeterminate value, not null. Write
int* p = nullptr;instead. - Using
0as a null pointer in new code. It compiles, but it hides the intent and can pick the wrong overload. - Dereferencing a null pointer. Reading or writing through it is undefined behavior. Dereferencing a null pointer is a common answer to the question what can cause a SEG fault? Check the pointer before you use it.
Summary
Use nullptr in modern C++. When you see NULL in older code, remember that it is usually 0. Initialize pointers to nullptr when they have no target yet, and check them before you use them.