Finding memory bugs
Off-by-one reads, leaks, double frees, use-after-free and heap overruns — and which tool catches which.
- Recognise the five classic memory bugs in the repository's examples
- Build with AddressSanitizer and read its report
- Know what AddressSanitizer misses, and when to reach for Valgrind or gdb
The debugger/ folder contains the same handful of bugs, written once and reused for each tool. They are worth knowing by sight, because none of them reliably crashes: most print plausible output and corrupt something quietly.
AddressSanitizer
AddressSanitizer (ASan) is built into g++ and clang. It surrounds every allocation with poisoned “red zones” and checks every memory access at run time, for roughly a 2× slowdown:
g++ -g -fsanitize=address 0_error.cpp -o 0_error
./0_error
Running each example in 02debug_with_address_sanitizer/ with g++ 13:
| File | Bug | ASan reports |
|---|---|---|
0_error.cpp | loop with i <= size() reads one past a std::array | stack-buffer-overflow |
1_error.cpp | two new float[1024], never deleted | detected memory leaks (LeakSanitizer) |
2_error.cpp | delete[] a; twice | attempting double-free |
3_error.cpp | reads a[0] after delete[] a | heap-use-after-free |
4_error.cpp | generate(a, a + N - 1, …) leaves the last element unset, then reads it | nothing |
Each report names the bad access, the line that made it, and where the memory was allocated and freed — usually enough to fix the bug without a debugger.
The last row is the important one. Reading uninitialised memory is not an address error — the memory is valid, just never written — so ASan cannot see it. That is what Valgrind’s Memcheck (the 01debug_with_valgrind/ versions, run as valgrind ./4_error) or clang’s MemorySanitizer are for. Valgrind needs no recompilation but runs 20–50× slower.
Two smaller observations from the same files: in 2_error.cpp the “vector addition” loop is written for (int i = 0; i > N; i++) and never runs, and b is never freed. Neither changes the lesson, but both are the kind of slip these tools are for.
A heap overrun that hits another array
04gdb_watchpoints/1_error.cpp is the most instructive example. It allocates two arrays of 1,024 ints, fills p_ones with ones, then writes random numbers into p_random — including one extra write:
array[1028] = rd(); // p_random has only 1024 elements
Without ASan, nothing crashes. Instead the program reports Incorrect values at index 0! for p_ones. The reason is the heap layout: each 1,024-int block is 4,096 bytes, and glibc’s allocator puts a 16-byte header before the next block. So p_random[1028] — 4,112 bytes from the start — is exactly p_ones[0]. The overrun silently overwrote a different array. With -fsanitize=address the same line is reported immediately as a heap-buffer-overflow.
Note also that the message only appears when stdout is a terminal: piped into a file, the line sits in std::cout’s buffer and is lost when assert aborts the program. std::cerr, which is unbuffered, is the right stream for diagnostics.
gdb
When the bug is in the logic rather than memory, a debugger lets you stop the program and look. The repository’s notes cover the essentials:
gdb ./1_error
(gdb) run # run until it crashes
(gdb) bt # backtrace: how did we get here?
(gdb) print divisor # inspect a variable
(gdb) break 21 if divisor==0 # stop only when the bug is about to happen
(gdb) watch p[0] # stop whenever this memory changes
03debug_with_gdb/1_error.cpp divides by a random number that is sometimes 0 — a crash that only happens on some runs. A conditional breakpoint catches it on the first run where it occurs. A watchpoint is the tool for the heap overrun above: watch p_ones[0] stops on the exact line in random() that changes it. The 06gdb_with_record_and_replay/ example goes one step further with rr, which records a run once and then replays it under gdb as often as needed — including backwards, from the crash to the instruction that caused it.
Try it yourself. Build 4_error.cpp with -fsanitize=address and confirm it prints a number without complaint. Then fix the generate call and add -fsanitize=undefined too. For 1_error.cpp in the gdb folder, set break 21 if divisor==0 and run until it triggers.