Part 1 of 7 · 1 min read
Why this page comes second
In this module you will
- Name the six common heap bugs and give a short program that shows each.
- Explain why a heap bug often runs without any visible error.
- Recognise a memory leak from the growth of the resident size, and find it with a tool.
- Build a program with AddressSanitizer and read its report.
- Run a program under Valgrind and read the report, and say what each tool costs.
Page 1 showed that malloc hands out pieces of a larger region, with headers and rounding around them. That design explains why many bugs do not crash: a write a little past the end of a block lands on bytes that belong to the allocator or to a neighbour, and the program goes on. This page lists the bugs, shows how they hide, and presents the two tools that find them. Page 3 turns to the stack, and page 8 returns to leaks on a live system. The sources are TLPI chapter 7, OSTEP chapter 14 and the manuals of AddressSanitizer and Valgrind. The test programs were compiled with gcc -g on glibc 2.39, and every output below was produced on a Linux machine.
About this example About this example
Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).
Scripted: The worked traces use small numbers chosen so they can be followed by hand.
Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.