C++ Academy · Lesson

Sanitizers Address Thread UB Sanitizer

Find memory, threading, and undefined behavior bugs with the sanitizer family.

Lesson 2 of 415 steps

Sanitizers Address Thread UB Sanitizer 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.

Runtime Checkers

Sanitizers are compile-time instrumented runtime checkers built into Clang and GCC. They catch bugs the type system cannot.

AddressSanitizer (ASan)

Detects memory errors: buffer overflows, use-after-free, double free, leaks. Slowdown ~2x.

g++ -fsanitize=address -g -O1 main.cpp
./a.out

Sample ASan Output

When ASan detects a bug, it prints a stack trace with the exact line and the type of error.

==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 4 at 0x60200000001c
    #0 0x... in main main.cpp:12

ThreadSanitizer (TSan)

Detects data races and other concurrency bugs. Slowdown ~5-15x.

g++ -fsanitize=thread -g main.cpp
./a.out
# Reports concurrent unprotected accesses

Undefined Behavior Sanitizer (UBSan)

Catches signed integer overflow, null pointer dereference, misaligned access, divide by zero, and more.

g++ -fsanitize=undefined -g main.cpp

MemorySanitizer (MSan)

Detects reads of uninitialized memory. Clang-only. Requires building the entire program (including libraries) under MSan.

LeakSanitizer

Bundled with ASan on Linux. Reports memory still allocated at exit.

ASAN_OPTIONS=detect_leaks=1 ./a.out

Sanitizers Are NOT Free

All sanitizers slow programs down and use more memory. Use them in development and CI — not in production releases.

Cannot Combine All

ASan, TSan, and MSan are mutually exclusive — they instrument memory differently. UBSan can combine with the others.

Compiler Support

GCC supports ASan, TSan, UBSan. Clang supports all of the above plus MSan. The flags are identical across compilers.

CI Integration

Run your test suite under each sanitizer in CI. Catch race conditions and memory bugs before they ship. Combine with fuzzing for the best coverage.

Tip: Build with -O1

Optimize at least at -O1 when building for sanitizers. -O0 works but inlines less, making stack traces less informative.

Suppression Files

For known false positives or third-party issues, write a suppression file and tell the sanitizer to ignore them.

ASAN_OPTIONS="suppressions=asan.supp" ./a.out

Quick Check

Which sanitizer catches data races between threads?

Recap

Sanitizers catch real runtime bugs: AddressSanitizer for memory errors, ThreadSanitizer for races, UBSan for undefined behavior, MSan for uninitialized reads. Combine with your test suite in CI for big safety wins.

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 “Sanitizers Address Thread UB Sanitizer” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Sanitizers Address Thread UB Sanitizer”, 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 “Sanitizers Address Thread UB Sanitizer”?

Find memory, threading, and undefined behavior bugs with the sanitizer family. 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 “Sanitizers Address Thread UB Sanitizer” 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. Static Analysis Tools clang-tidy cppcheck
  2. Sanitizers Address Thread UB Sanitizer
  3. Fuzzing with libFuzzer
  4. Continuous Integration for C++ Projects
← Back to C++ Academy