Valgrind Basics
Detect leaks and errors.
Valgrind Basics is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Valgrind?
Valgrind is an instrumentation framework. Its default tool, Memcheck, detects memory leaks and errors without recompiling, by running your program on a synthetic CPU.
Running Memcheck
Invoke Valgrind in front of your program. Build with -g first for line numbers.
g++ -g main.cpp -o app
valgrind --leak-check=full ./appDetecting Leaks
Memcheck reports memory still allocated at exit, classifying it as definitely lost, indirectly lost, or still reachable.
#include <cstdlib>
int main() {
int* p = (int*)malloc(40); // never freed -> definitely lost
return 0;
}Leak Categories
Understand the report categories.
- Definitely lost: no pointer remains; a real leak
- Indirectly lost: reachable only via a lost block
- Still reachable: pointer exists at exit (often benign)
Invalid Reads and Writes
Memcheck flags access to unallocated or freed memory, reporting the address and size.
#include <cstdlib>
int main() {
int* a = (int*)malloc(2 * sizeof(int));
a[5] = 1; // invalid write past the block
free(a);
return 0;
}Uninitialized Values
Memcheck warns when a conditional or output depends on an uninitialized value, which is a common source of nondeterministic bugs.
#include <iostream>
int main() {
int x; // uninitialized
if (x == 42) // use of uninitialized value
std::cout << "hi\n";
return 0;
}Reading the Output
Each error block shows the operation, the offending stack trace, and (for heap blocks) where the memory was allocated. The HEAP SUMMARY totals allocations and leaks.
Useful Flags
Common options:
--leak-check=full: detail each leak--show-leak-kinds=all--track-origins=yes: trace uninitialized values to their source--error-exitcode=1: fail CI on errors
Valgrind vs ASan
Both find memory bugs, with trade-offs.
- Valgrind: no recompile needed, slower (10-50x), catches uninitialized reads well
- ASan: needs recompile, much faster, better stack-overflow detection
Suppressing Known Issues
Third-party libraries may produce noise. Generate and apply a suppression file with --gen-suppressions=all and --suppressions=file to silence known-benign reports.
When to Use Valgrind
Reach for Valgrind when you cannot recompile with sanitizers, need precise uninitialized-value tracking, or want a second opinion. Otherwise ASan is usually faster for routine CI.
Quick Check
Recall the leak categories.
Recap
You learned Valgrind basics.
- Memcheck finds leaks, invalid access, uninitialized reads, no recompile
- Run with
--leak-check=full --track-origins=yes - Leak kinds: definitely / indirectly lost, still reachable
- Slower than ASan but great for uninitialized-value bugs
Frequently asked questions
Is the “Valgrind Basics” lesson free?
Yes — the full text of “Valgrind Basics” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Valgrind Basics”?
Detect leaks and errors. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Valgrind Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using gdb and lldb
- AddressSanitizer
- UBSan and TSan
- Valgrind Basics