Ch5.4: nullptr

Overview

A pointer must always store either:

In this chapter, you will learn:

1. What is nullptr?

nullptr is a special value that means:

“this pointer does not point to any object.”


int *p = nullptr;   // p points to nothing

This is the correct way to represent an empty or uninitialized pointer.

2. Why nullptr exists

Before C++11, programmers used 0 or NULL to represent a null pointer. These had problems:

nullptr fixes all of these issues. It has its own type:


std::nullptr_t

and it converts only to pointer types.

3. Always initialize pointers with nullptr

An uninitialized pointer contains an unpredictable address. Using it is undefined behavior.


// Bad: uninitialized pointer
int *p;

// Good: always initialize
int *q = nullptr;

Later, when you have a real object, assign its address:


#include 

int value{42};
int *p = ::std::addressof(value);

4. nullptr has no address

A null pointer does not refer to any memory location.


p:
Address:   4000
Memory:   [ null ]   ← p stores “no address”

The pointer itself lives somewhere in memory, but its value is “no address”.

5. Printing nullptr

In fast_io, printing a nullptr_t value directly produces the string "nullptr".


#include <fast_io.h>
#include <cstddef>

std::nullptr_t np = nullptr;
println(np);     // prints: nullptr

If you want the numeric representation of a null pointer, use pointervw:


#include <fast_io.h>
using namespace ::fast_io::iomnp;

println(pointervw(nullptr));

This prints the full pointer-width numeric address, zero‑extended to the platform’s pointer size. For example, on a 64‑bit system:


0x0000000000000000

This ensures consistent formatting for all pointers, including null ones.

6. You cannot dereference nullptr

A null pointer does not point to any object. You cannot dereference nullptr.


int *p = nullptr;

// *p = 10;   // undefined behavior — never do this

Dereferencing nullptr is always undefined behavior.

7. Undefined behavior does NOT guarantee a crash

Many beginners assume that dereferencing nullptr will always crash. This is false.

Undefined behavior means:

Compilers may assume pointers are never null

Modern compilers aggressively optimize code. They may assume that a pointer is never null and remove checks or reorder code.

WebAssembly without memory tagging will NOT trap

On WebAssembly, if memory tagging is not enabled, dereferencing nullptr often does not trap. The VM cannot efficiently detect null dereferences, so the program may continue running with corrupted state.

This is why dereferencing nullptr is extremely dangerous.

8. nullptr in comparisons

You can check whether a pointer is null:


int *p = nullptr;

if (p == nullptr)
{
    // p does not point to anything
}

This is often used before dereferencing:


if (p != nullptr)
{
    int x = *p;   // safe
}

9. nullptr and pointer-to-pointer

Pointer-to-pointer types can also be null.


int **pp = nullptr;

// **pp;   // undefined behavior — pp does not point to a pointer

10. Use sanitizers to detect null dereferences

Modern compilers provide tools that detect null dereferences at runtime.

AddressSanitizer


-fsanitize=address

Detects:

Memory Tagging (WebAssembly)


-fsanitize=memtag

Detects:

These tools make pointer bugs easier to find and fix.

Key takeaways