C++ Reference ยท Keywords and Statements
The nullptr literal
Reference for C++ nullptr: the type-safe null pointer literal of type std::nullptr_t, why it replaces NULL and 0, overload resolution, comparisons and templates.
nullptr (C++11) is the null pointer literal. It has its own type, std::nullptr_t, and converts implicitly to any pointer type and to any pointer-to-member type, but not to integers (other than bool in direct contexts). That makes it safer than the old alternatives 0 and NULL, which are integers and can pick the wrong function overload.
A null pointer points to no object. Comparing against it is always safe; dereferencing it is undefined behavior.
int* p = nullptr; if (p == nullptr) { /* nothing to point at */ } if (!p) { /* same test */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
nullptr // prvalue of type std::nullptr_t std::nullptr_t // in <cstddef> T* p = nullptr; // any object, function or member pointer
| Property | Description |
|---|---|
| Type | std::nullptr_t; sizeof equals sizeof(void*) |
| Converts to | Every pointer type, pointer-to-member type, and bool in direct-initialization or conditions |
| Does not convert to | Integer types |
| Comparisons | == and != with any pointer; nullptr == nullptr is true |
Usage notes
- Use
nullptreverywhere C usedNULLor0for pointers. In C++NULLis an implementation-defined null pointer constant that is often just0or0L, which is whyf(NULL)can callf(int). - Dereferencing a null pointer, or calling a non-static member function through it is undefined behavior;
delete pandfree(p)with a nullpare fine and do nothing. Always check a pointer you did not create. - Prefer references when null is not a valid state; prefer
std::optional<T>for "maybe a value"; use smart pointers (which compare againstnullptr) for owners. - Pointers inside a freshly created object are indeterminate unless you initialize them: write
T* p = nullptr;or use a default member initializer. - A
std::nullptr_tparameter lets a function (or smart-pointer constructor) accept the literal explicitly, asstd::unique_ptr(nullptr_t)does. - Null pointer values are not guaranteed to have all bits zero on every architecture, so do not use
memsetto create null pointers. - In templates,
nullptrdeduces asstd::nullptr_t, not as a pointer type, so a deducedTwill not be a pointer. Overload onstd::nullptr_tor pass an explicitly typed pointer when that matters. - In C23,
nullptris also a keyword with typenullptr_t.
Related
- Pointers
- unique_ptr
- new and delete
- Tutorial: Pointers, Dynamic memory