Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is a Null Pointer in C++? nullptr Explained

C++
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 24, 2023
UpdatedNov 17, 2026
7 min read

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.

Table of Contents

  • What a null pointer is
  • How to create and test a null pointer
    • Use nullptr
    • Test a pointer in a condition
  • Why null pointers matter
    • Initialise pointers you cannot set yet
    • Signal "not found" or "no value"
    • Mark the end of linked structures
  • What happens when you dereference a null pointer
  • Safer alternatives to raw null pointers
  • Frequently Asked Questions
    • Is a null pointer the same as a pointer to zero?
    • Should I still use NULL in C++?
    • What is the difference between a null pointer and a dangling pointer?
    • Does delete on a null pointer crash?
  • Summary

What a null pointer is

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".

cpp

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.

How to create and test a null pointer

Use nullptr

nullptr 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.

SpellingStatusProblem
nullptrRecommended since C++11None, it has its own type
NULLLegacy macroExpands to an integer constant such as 0 or 0L, so it can select an int overload
0LegacyA plain integer that happens to convert to a pointer
cpp

Test a pointer in a condition

A pointer converts to bool in a condition: null becomes false, anything else becomes true. These checks are equivalent:

cpp

Pick one style and use it consistently across a code base.

Why null pointers matter

Initialise pointers you cannot set yet

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.

cpp

Signal "not found" or "no value"

Functions that search often return a pointer and use nullptr for a miss:

cpp
text

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.

Mark the end of linked structures

Linked lists, trees and graphs commonly use nullptr for "no next node" or "no child".

What happens when you dereference a null pointer

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.

cpp

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.
  • Calling a non-virtual member function through a null pointer is still undefined behavior, even if the function never touches the object's data.

Safer alternatives to raw null pointers

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:

NeedPrefer
A parameter that must always refer to an objectA reference, T& or const T&
A value that may be absent, stored by valuestd::optional<T>
Exclusive ownership of a heap objectstd::unique_ptr<T>
Shared ownershipstd::shared_ptr<T>
A non-owning view of a sequencestd::span<T> (C++20) or std::string_view

Smart pointers can themselves be empty, and they compare with nullptr:

cpp

Create them with std::make_unique and std::make_shared rather than calling new directly.

Frequently Asked Questions

Is a null pointer the same as a pointer to zero?

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.

Should I still use 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.

What is the difference between a null pointer and a dangling pointer?

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.

Does 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.

Summary

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.

Share