C++ Academy · Lesson

AddressSanitizer

Catch memory errors.

Lesson 2 of 413 steps

AddressSanitizer is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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
./app

Heap 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
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “AddressSanitizer” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “AddressSanitizer”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

  1. Using gdb and lldb
  2. AddressSanitizer
  3. UBSan and TSan
  4. Valgrind Basics
← Back to C++ Academy