For a practical first step, add AddressSanitizer to a dedicated development or test build, compile and link every relevant target with the sanitizer enabled, then run your tests under that build. With Clang or GCC, start with -fsanitize=address; with MSVC, use /fsanitize=address. Add other sanitizers only for the bug classes and configurations they support. These tools detect problems on paths your tests execute; safer buffer APIs help reduce risks that runtime checks alone cannot prevent.
What the main C and C++ sanitizers detect
Sanitizers are compiler-assisted runtime checks, not a single comprehensive memory-safety switch. They target different classes of defects, and each has toolchain, platform, instrumentation and deployment constraints. Check the current manual for the compiler version and target you use.
As an Amazon Associate I earn from qualifying purchases.
| Sanitizer | Primary purpose | Practical constraint |
|---|---|---|
| AddressSanitizer (ASan) | Detects invalid memory accesses, including out-of-bounds accesses and use-after-free, in supported configurations. | Instrument and link consistently, then execute relevant tests. Supported targets and runtime options vary by compiler. |
| UndefinedBehaviorSanitizer (UBSan) | Checks selected forms of undefined behavior, such as signed integer overflow and invalid shifts. | Checks can be selected individually. Review compiler-specific support and combinations. |
| MemorySanitizer (MSan) | Looks for uses of uninitialized values. | Reliable use calls for instrumenting program code and, where possible, dependencies. Partial coverage can make reports unreliable. |
| ThreadSanitizer (TSan) | Detects data races. | It is not a general bounds or memory-lifetime sanitizer, and some sanitizer combinations are unsupported. |
Clang and GCC document ASan and UBSan options; GCC also documents which combinations it supports. Clang documents MSan’s instrumentation requirements and supported platforms. See the Clang AddressSanitizer manual, GCC instrumentation options, Clang UndefinedBehaviorSanitizer manual, and Clang MemorySanitizer manual.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Enable AddressSanitizer with your compiler
Clang or GCC
Pass -fsanitize=address when compiling and when linking. Use the compiler driver for the link step so it can arrange the sanitizer runtime correctly. Apply the option to the project libraries and test executables involved, not just one source file. The exact build-system syntax depends on your project; the important requirement is that compilation and linking are both instrumented.
#1 Best Overall
Clang’s manual describes runtime options, including use-after-return checks, and notes that leak-detection behavior varies by platform. GCC’s instrumentation manual documents its AddressSanitizer behavior and limitations, including combinations that cannot be used together in documented configurations. Check those manuals for your compiler version and target rather than assuming flags and runtime behavior are identical.
Microsoft Visual C++
Microsoft documents AddressSanitizer for MSVC with /fsanitize=address. Add /Zi when you want debug information to improve stack traces. Microsoft says the documented configuration works with its listed optimization levels and static or dynamic CRT options, but does not support Profile-Guided Optimization and is not intended for production use. Confirm availability and restrictions for your installed Visual Studio/compiler version and target in Microsoft’s AddressSanitizer documentation.
Roll the checks into a development or test build
- Create an opt-in configuration. Add sanitizer flags to a named development, test or CI configuration instead of silently changing every release build.
- Instrument compilation and linking. Enable the chosen sanitizer for relevant project libraries and test binaries, and use the compiler driver for linking.
- Run meaningful workloads. Execute unit and integration tests, plus representative workloads that exercise risky or important code paths. Instrumentation cannot report a path the run does not reach.
- Capture and act on diagnostics. Configure CI to preserve sanitizer output and handle findings according to the project’s failure policy.
- Add other checks deliberately. Run UBSan in a separate or compatible configuration when you want checks for selected undefined behavior. Consult the relevant compiler manual for supported checks and combinations.
- Keep release hardening separate. Treat sanitizers as testing tools unless the documentation for the specific tool and intended use supports deployment. Review production security settings independently.
When to add UBSan or MSan
UndefinedBehaviorSanitizer
Use -fsanitize=undefined when you want runtime checks for selected undefined behavior in addition to memory-addressability checks. Clang lists checks such as signed integer overflow and invalid shifts. Its documentation explains that linking with the compiler driver supplies the UBSan runtime unless trap mode is used, and lists supported platforms including Linux, macOS, Windows, Android and several BSD systems. GCC’s supported checks, combinations and runtime behavior are documented separately; verify them for the version used by your project. See Clang’s UBSan manual and GCC’s instrumentation options.
MemorySanitizer
Clang’s -fsanitize=memory targets uses of uninitialized values, but is harder to deploy broadly than ASan. Clang advises instrumenting all program code and dependent libraries where possible; incomplete instrumentation can make results unreliable. Its manual lists Linux, NetBSD and FreeBSD support and says the runtime is intended for testing rather than production executables. Clang documents memory overhead of 2× real memory without origin tracking and 3× with origin tracking; those figures describe Clang MSan, not sanitizers generally. Consult the MemorySanitizer manual before adopting it.
Reduce unsafe buffer operations in the source
Runtime instrumentation helps find defects exercised in a run; source-level practices can make bounds more explicit before runtime. Clang’s Safe Buffers guidance explains that raw pointers do not inherently carry formal bounds information, making it difficult for a compiler to verify that an access stays within the intended object.
Clang’s -Wunsafe-buffer-usage warning can flag raw-pointer indexing, pointer arithmetic and bounds-unsafe functions such as std::memcpy() for review. Consider replacing raw pointer-and-length interfaces with bounds-carrying containers, views or iterators where practical. Review custom containers, views and dependencies too: safety depends on consistent use and suitable hardening throughout the code. The Clang C++ Safe Buffers guide explains the model and its limits.
Quick Recap
Best Value
Choose configurations by coverage and constraints
- Bug class: ASan targets addressability and lifetime faults; MSan targets uses of uninitialized values; UBSan checks selected undefined operations; TSan targets races.
- Coverage: All runtime checks depend on executed paths. MSan additionally has demanding requirements for instrumenting dependencies.
- Platform and toolchain: Support differs by compiler, operating system, architecture and version. Confirm each against the relevant current manual.
- Build and runtime cost: Instrumentation can affect runtime, memory use or binary size. Clang’s MSan overhead figures are specific to that sanitizer and its origin-tracking setting.
- Compatibility and deployment: Some sanitizers cannot be combined, and testing runtimes or options may not suit production. Keep test instrumentation distinct from release hardening.
- Detection versus prevention: Sanitizers find exercised faults; safer APIs and coding conventions help make bounds available and reduce unsafe operations.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →




