Ch5.4: nullptr
Overview
A pointer must always store either:
- the address of a valid object, or
- nullptr, meaning “no address”.
In this chapter, you will learn:
- what
nullptrmeans - why
nullptris safer than older null-pointer constants - how to initialize pointers with
nullptr - why dereferencing
nullptris undefined behavior - why undefined behavior does not guarantee a crash
- how sanitizers help detect null dereferences
- how
fast_ioprintsnullptrandnullptr_t
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:
0is an integer, not a pointerNULLis often defined as0- overload resolution could choose the wrong function
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:
- the program might crash
- or it might corrupt memory
- or it might continue running as if nothing happened
- or the compiler may optimize away code based on incorrect assumptions
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:
- null dereferences
- use‑after‑free
- out‑of‑bounds accesses
Memory Tagging (WebAssembly)
-fsanitize=memtag
Detects:
- dangling pointer dereferences
- invalid memory accesses
These tools make pointer bugs easier to find and fix.
Key takeaways
nullptrmeans “this pointer does not point to any object”.- Always initialize pointers with
nullptrif they have no target yet. - You cannot dereference nullptr.
- Dereferencing nullptr is undefined behavior.
- Undefined behavior does NOT guarantee a crash.
- Compilers may assume pointers are never null.
- WebAssembly without memory tagging will not trap on null dereference.
fast_ioprintsnullptras the string"nullptr".pointervw(nullptr)prints the full pointer-width numeric address.- Use
-fsanitize=addressor-fsanitize=memtagto detect null dereferences.