Sanitizers Address Thread UB Sanitizer
Find memory, threading, and undefined behavior bugs with the sanitizer family.
Sanitizers Address Thread UB Sanitizer 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.
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.outSample 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:12ThreadSanitizer (TSan)
Detects data races and other concurrency bugs. Slowdown ~5-15x.
g++ -fsanitize=thread -g main.cpp
./a.out
# Reports concurrent unprotected accessesUndefined Behavior Sanitizer (UBSan)
Catches signed integer overflow, null pointer dereference, misaligned access, divide by zero, and more.
g++ -fsanitize=undefined -g main.cppMemorySanitizer (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.outSanitizers 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.outQuick 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.
Frequently asked questions
Is the “Sanitizers Address Thread UB Sanitizer” lesson free?
Yes — the full text of “Sanitizers Address Thread UB Sanitizer” 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 “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; 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 “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
- Static Analysis Tools clang-tidy cppcheck
- Sanitizers Address Thread UB Sanitizer
- Fuzzing with libFuzzer
- Continuous Integration for C++ Projects