AddressSanitizer
Catch memory errors.
AddressSanitizer is a free C++ Academy lesson on CoddyKit — lesson 2 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 AddressSanitizer?
AddressSanitizer (ASan) is a compiler-based tool that detects memory errors at runtime: out-of-bounds access, use-after-free, double-free, and leaks. It is fast enough for everyday testing.
Enabling ASan
Add -fsanitize=address at both compile and link time, plus -g for readable reports.
g++ -fsanitize=address -g -O1 main.cpp -o app
./appHeap Buffer Overflow
ASan flags reads/writes past an allocated buffer. This program writes one element too far.
#include <cstdlib>
int main() {
int* a = (int*)malloc(3 * sizeof(int));
a[3] = 7; // out of bounds
free(a);
return 0;
}Use After Free
Accessing memory after it is freed is undefined behavior; ASan reports a heap-use-after-free with the freeing and allocation stacks.
#include <cstdlib>
int main() {
int* p = (int*)malloc(sizeof(int));
free(p);
*p = 5; // use after free
return 0;
}Stack Buffer Overflow
ASan also instruments stack arrays, catching overruns of local buffers.
#include <iostream>
int main() {
int buf[3] = {0, 0, 0};
for (int i = 0; i <= 3; ++i) buf[i] = i; // i==3 overflows
std::cout << buf[0] << "\n";
return 0;
}Reading the Report
An ASan report names the error type, the faulting address, and stack traces for the bad access plus the relevant allocation/free. The first stack frame usually points right at the bug.
Leak Detection
ASan includes LeakSanitizer on many platforms, reporting memory never freed at program exit, with the allocation stack.
#include <cstdlib>
int main() {
int* leaked = (int*)malloc(100); // never freed
return 0;
}Runtime Options
Tune behavior with the ASAN_OPTIONS environment variable, e.g. ASAN_OPTIONS=detect_leaks=1:halt_on_error=0.
Performance Cost
ASan typically slows programs about 2x and increases memory use. That is acceptable for tests and CI, but you would not ship a release build with it enabled.
Combine with Tests
Run your unit tests under ASan in CI. Many real bugs only surface when exercised by tests with sanitizers on. Keep a dedicated sanitizer build configuration.
Not a Substitute for Care
ASan finds bugs only on code paths you actually execute. Pair it with good test coverage; an unexercised buggy branch stays hidden.
Quick Check
Recall how to enable ASan.
Recap
You learned AddressSanitizer.
- Detects out-of-bounds, use-after-free, double-free, leaks
- Enable with
-fsanitize=address -g - Reports name the error and show allocation/free stacks
- ~2x slower; ideal for tests and CI, not releases
Frequently asked questions
Is the “AddressSanitizer” lesson free?
Yes — the full text of “AddressSanitizer” 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 “AddressSanitizer”?
Catch memory 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “AddressSanitizer” 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