← All posts
November 28, 2026 · 2 min read

Why dereferencing a null pointer crashes in C++

A null pointer in C++ is 8 zero bytes: address 0. On x86-64 Linux nothing is mapped there, so *p faults and the kernel sends SIGSEGV.

A null pointer in C++ is a pointer that holds no object's address. You write it as nullptr. Reading through one, *p, is undefined behaviour in the language; on x86-64 Linux, in practice, the process is killed with a segmentation fault. This walks through why, byte by byte, with one real run of a ten-line program.

Everything below comes from that program, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) and run on x86-64 Linux.

What is inside a null pointer?

int* p = nullptr; is an ordinary 8-byte pointer variable. The program copies its bytes out with std::memcpy and prints them: all eight are 00.

int* p = nullptr shown as a hex dump: eight bytes, all 00

Printed with %p, glibc shows a null pointer as (nil), and p == nullptr is 1. The keyword itself has its own type, std::nullptr_t, which converts to any pointer type.

Where does it point?

A pointer's bytes are an address. Eight zero bytes are address 0x0. (The C++ standard does not say a null pointer must be all zero bits, but on x86-64 Linux it is, and the program shows it.)

An arrow from p's eight zero bytes down to the page at address 0x0000 to 0x0fff: p = address 0x0

What does *p actually do?

*p on an int* means: load 4 bytes from the address in p. GCC at -O2 compiles the line to exactly that, a load from absolute address 0:

mov    0x0,%eax

*p tells the CPU to load 4 bytes at 0x0; g++ -O2 compiles it to mov 0x0,%eax

Why does the load fail?

The CPU does not use that address directly. The MMU translates it through the process's page tables, one 4096-byte page at a time. Address 0 sits in the page 0x0000–0x0fff, and Linux leaves it unmapped: on the machine used here /proc/sys/vm/mmap_min_addr is 4096, so normal processes cannot map anything below it, and the lowest mapping of the process starts far above 0.

The page 0x0000 to 0x0fff turns berry and reads not mapped: the MMU finds no page at 0x0

With no page there, the load raises a page fault. The kernel finds no mapping for the address and sends the process SIGSEGV, signal 11. A second check program caught the signal and printed what the kernel reported: signal 11, si_code SEGV_MAPERR (address not mapped), fault address (nil).

What does the shell show?

The default action for SIGSEGV is to kill the process. Bash prints Segmentation fault, and the exit status of a process killed by a signal is 128 plus the signal number: 128 + 11 = 139.

The real terminal session: the eight 00 bytes, p (nil), p == nullptr 1, Segmentation fault, and exit status 139 = 128 + 11, above the program null.cpp

step what happens
int* p = nullptr; 8 bytes, all 00: address 0
*p the CPU loads 4 bytes from 0x0
MMU no page mapped at 0x0000–0x0fff
fault the kernel sends SIGSEGV, signal 11
shell Segmentation fault, exit status 139

Is the crash guaranteed?

No. The C++ standard calls dereferencing a null pointer undefined behaviour, so it promises nothing. The crash is what this platform does when the load really happens. Other compilers, flags or platforms can do something else. Check a pointer before you dereference it.

The program and its output

#include <cstdio>
#include <cstring>
int main() {
    int* p = nullptr;
    unsigned char b[8];
    std::memcpy(b, &p, sizeof p);
    for (unsigned char v : b) printf("%02x ", v);
    printf("\np %p\n", (void*)p);
    printf("p == nullptr %d\n", p == nullptr);
    printf("*p %d\n", *p);
}

Run in a terminal:

$ ./null
00 00 00 00 00 00 00 00 
p (nil)
p == nullptr 1
Segmentation fault
$ echo $?
139

The last printf never prints: the process dies on the load of *p, before printf is called.

Try it

What is sizeof nullptr? Work it out from what nullptr has to convert to, then print sizeof(nullptr) and sizeof(std::nullptr_t) and check.