C memory: malloc, free, and finding leaks with Valgrind
12 steps · about 3 h 37 min · Beginner
- C in 100 Seconds: Memory Leaks (article, codegiz, 8 min)
A quick overview of the rule that every malloc needs one free, plus the common prevention patterns, so you have the idea before the tools.
- Memory Leaks in C - Complete Guide to Detection and Prevention (article, learnmandu, 15 min)
Lists the usual causes (reassigned pointers, early returns, realloc failure) so you know what to look for.
- Runnable C Docs: Memory Leaks (doc, Coddy, 15 min)
Walks through three concrete leaks and their fixes, with the first Valgrind command to run.
- Valgrind Tutorial (tutorial, University of Wisconsin, 20 min)
A short course-handout tutorial that shows the Valgrind output for clean code, uninitialised reads and leaks, so you can recognise each one.
- Basic Valgrind Tutorial (tutorial, Baylor University, 15 min)
Explains how to read leak stack traces and compile with -g to get line numbers, and lists what Memcheck detects.
- Guide to Success on How to Use Valgrind (guide, Carnegie Mellon University (15-122), 25 min)
Moves from leaks to invalid reads, writes and invalid frees, and shows how to deduce the bug from the output.
- Lab 5: Memory Leaks and Valgrind (tutorial, UC San Diego CSE 29, 45 min)
A hands-on lab where you find and fix real leaks until Valgrind reports a clean run.
- Detecting and Fixing Memory Leaks with Valgrind (article, Hisam Mehboob, 10 min)
Gives the full flag set (--leak-check=full, --track-origins=yes) and a fixed-code example you can reproduce.
- AddressSanitizer · google/sanitizers Wiki (reference, Google (sanitizers project), 20 min)
The original source for ASan, which is a faster compile-time alternative to Valgrind that also catches use-after-free and overflows.
- Address Sanitizer (article, University of Rochester, 20 min)
Course reading with small examples of heap buffer overflow and use-after-free to compile and run yourself.
- How to read an AddressSanitizer report, step by step (article, Safeguard, 12 min)
Teaches you to read the 'freed by' and 'allocated by' stacks and trace the bug back to ownership.
- AddressSanitizer: what it catches, what it misses, what it costs (article, Safeguard, 12 min)
Last step: learn the limits of each tool so you know when to use Valgrind and when to use ASan.