Concepts / Pointers and References

Pointers and References

A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.

  • Programming

Testing the Belief That nullptr Is Zero

Many learners meet nullptr and quietly file it away as a special kind of zero. The reasoning feels natural: nullptr is a word that means nothing is there, zero is the number that means nothing is there, so the two must be the same kind of thing. This article tests that belief. The short version is that nullptr belongs to the family of pointer values, not the family of integers, and that difference decides whether a line of code compiles at all.

What do you think happens?

If nullptr were a zero-like integer, the line below would be a normal declaration. What does the compiler do with it: int x = nullptr;

  • It accepts the line and sets x to 0

  • It reports a compile error

  • It accepts the line and leaves x holding an unspecified value

Reveal answer
Answer:

It reports a compile error.

nullptr is a pointer value, not an integer. Assigning it to an int is an error, so the belief that nullptr is a zero-like integer does not hold.

Following a Pointer from Object to nullptr

A pointer's value is what decides what it refers to. Start with a pointer that refers to an ordinary object. Later, the program assigns the pointer the value nullptr. At that moment the pointer still exists and still holds a value, but the value now says that no object is being referred to. Follow the two steps in the diagram below and notice what state the pointer is in after each one.

pointer prefers to an objectp = nullptrassignmentpointer pholds nullptr, refers to noobjectprogram assignsp now holds
How does a pointer move from referring to an object to holding nullptr, and what state is it in after each step?

The pointer is still a perfectly good variable with a value. Only the meaning of that value has changed: it now says that no object is present. A pointer in this state is called a null pointer.

What a Null Pointer Is For

Definition

A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.

The goal behind pointers is that a pointer should always point to an object, so that using it through a dereference is valid. Real programs cannot always meet that goal. Sometimes there is no object to point to yet. Sometimes the program needs a way to express that no object is available, such as marking the end of a linked list. For those situations the pointer is given the value nullptr.

Pointers of different types still share the same null value. The source states that there is only one nullptr, shared by all pointer types. A pointer to a list link and a double pointer both use that same null value. The type of a pointer still determines what it could point to, but the null value itself is common to every pointer type.

pointer pholds nullptrno objectnothing on the far sidepointer qrefers to an objectobjectan ordinary objectrefers to nothingrefers to
How does a pointer holding nullptr differ from one holding the address of an object, and what does each point to?

The diagram is deliberately simple. It shows the relationship that matters here: a null pointer has no object on its far side, while a valid pointer does. It makes no claim about where in memory either item lives, because the source does not describe that.

nullptr Is a Pointer Value, Not an Integer

The original belief fails at exactly this point. nullptr is a pointer value, not an integer, so it cannot be assigned to an int. The two declarations below have the same shape, but only the second one is valid, because it declares a pointer whose value is nullptr.

cpp
Notes on the code
  • The first line fails to compile because nullptr cannot be stored in an int.
  • The second line is valid because the variable is a pointer, and nullptr is a pointer value.
PropertyZero as an integernullptr
Family of valuesIntegersPointer values
Can be assigned to an intYesNo, this is a compile error
Can be assigned to a pointerNoYes
MeaningThe number zeroA pointer that refers to no valid object

Why nullptr and the integer zero are not interchangeable

Checking a Pointer Before Dereferencing

A null pointer is useful because it lets a program say that no object is present. That only helps if the program looks at the value before relying on it. The source recommends this habit directly: it is often wise to check that a pointer argument that is supposed to point to something actually points to something. Validating a pointer means testing it against nullptr before the dereference, so that code acts on the pointer only when it refers to an object.

pointer pready to usep is nullptr?compare beforedereferencingskip dereferenceno object to usecontinuedereference pp refers to an objectyesno
What path does the program take when a pointer is nullptr versus when it refers to an object, and where is the dereference skipped?
cpp
Notes on the code
  • The comparison p != nullptr decides whether the dereference is reached.
  • When p holds nullptr, the body of the first if is skipped, so the dereference never happens.
  • When q refers to an object, the same check lets the dereference run.
Worked example

Deciding whether a dereference runs

A function receives a pointer p. Show which branch runs when p holds nullptr and when p refers to an object.

  1. 1
    Compare the pointer

    Evaluate p != nullptr before any dereference is attempted.

  2. 2
    Case one: p holds nullptr

    The comparison is false, so the dereference is skipped and the program continues without using p.

  3. 3
    Case two: p refers to an object

    The comparison is true, so the dereference runs and the object is used through p.

Result

The dereference runs only when p refers to an object, which is exactly the protection the check is meant to provide.

Mistakes Beginners Make with nullptr

  • Common mistake

    Treating nullptr as the integer zero

    What it looks like
    int count = nullptr;
    Why it fails

    nullptr is a pointer value, and assigning it to an int is an error.

    Do this instead
    Store a null value only in a pointer, such as int* p = nullptr;
  • Common mistake

    Dereferencing a pointer without checking it first

    What it looks like
    int* p = nullptr;
    *p = 20;
    Why it fails

    A null pointer does not refer to a valid object, so using it through a dereference is not valid.

    Do this instead

    Compare with nullptr first, and dereference only inside the branch where the pointer refers to an object.

  • Common mistake

    Assuming all pointer types need different null values

    Why it fails

    The source states that there is only one nullptr, shared by all pointer types.

    Do this instead

    Use nullptr for any pointer type; the type of the pointer still controls what it could point to.

Predicting the Branch for a Null Pointer

EASY

Consider the code below. Before running it or reading further, decide whether the line marked as a dereference executes, and explain why. Then decide whether the program would be valid if the first line were changed to int* p = nullptr; with no check at all.

int value = 5;
int* p = nullptr;
if (p != nullptr) {
    *p = 7;
}
Hints
  • Ask what value p holds on the first line of the if statement.
  • Ask whether the comparison with nullptr is true or false for that value.
  • Think about the difference between a null pointer and a pointer that refers to an object.

Key Takeaways

  • A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
  • nullptr is a pointer value, not an integer, so assigning it to an int is a compile error.
  • There is one nullptr shared by all pointer types, and it is used when no object is available, such as marking the end of a linked list.
  • Compare a pointer with nullptr before dereferencing it, so the dereference runs only when the pointer refers to an object.

More concepts starting with P