Skip to main content

Section 18.5 Null Pointers

Pointers generally β€œpoint” at a piece of memory by storing its memory address. However, there are times when we will want to have a pointer point at nothing. Perhaps we want to declare a pointer, but not immediately assign it a value. Or we know that the memory a pointer points at is now gone and we want to stop pointing at that memory. We can set a pointer to point at nothing by assigning it the value nullptr. This is known as a null pointer . null pointer
int* p = nullptr;
nullptr is really just the value 0. 0 is never a valid memory address for a program to try to dereference, so if a pointer has the value 0, we know it isn’t pointing at anything. You can’t use a null pointer to do any useful work, as it doesn’t point at anything. However, it is easy to verify that a pointer that is storing nullptr is not valid. The following if statement could be used to guard against a mullptr and only use p if it is not null:
if (p != nullptr) {
    /* do something */
}
And, because nullptr is just the value 0, which is equivalent to false in a boolean context, and every other value is equivalent to true, we can also write the if statement as:
if (p) {
    /* do something */
}
If p is null, then the if looks like if (0) which is false. If p has some other memory address, the if looks like if (823482340) which is true no mater what the number is. So if (p) checks to see that the pointer is not null. (That doesn’t mean it actually points at the right thing!) If you want to check if a pointer is null, you can use either of these forms:
Listing 18.5.1.
if (p == nullptr) {
    /* do something because pointer is null */
}
if (!p) {
    /* do something because pointer is null */
}
A null pointer is not the same as an uninitialized pointer. An uninitialized pointer is a pointer that has not been assigned any value at all. It may contain any random value, including a valid memory address. Dereferencing an uninitialized pointer can lead to undefined behavior, which can cause your program to crash or behave unpredictably. A null pointer, on the other hand, is explicitly set to point at nothing, and dereferencing it will always result in an immediate error. It is also easy to check if a pointer is null. But there

Warning 18.5.1.

Any pointers you do not initialize immediately should be set to nullptr. If you do something that invalidates a pointer (delete the thing it points at), you should set the pointer to null.

Checkpoint 18.5.1.

What statements are true about the difference between a null pointer and an uninitialized pointer?
  • A null pointer is explicitly set to point at nothing, while an uninitialized pointer may contain any random value.
  • Null pointers are always valid memory addresses, while uninitialized pointers are not.
  • Null pointers can be dereferenced safely, while uninitialized pointers cannot.
  • It is easy to check if a pointer is null, while it is hard to tell if a pointer has a bad address because it was uninitialized.
You have attempted of activities on this page.