π‘οΈ Debugging Tools for C++ You Should 100% Use
π§Ό 1. AddressSanitizer (ASan)
-
You already know the girl. Sheβs protective, loud, and dragging every invalid memory access by its wig.
-
Just compile with:
g++ -fsanitize=address -g -O1 your_code.cpp -o output
π₯ 2. Valgrind (The Classic Queen)
-
Sheβs slower, but ultra-detailed. Great for memory leaks, use-after-free, uninitialized memory reads.
-
Use like:
valgrind ./output
- This flag tells Valgrind to trace the origin of unitialized values.
valgrind --track-origins=yes ./output
Bonus: combine with --leak-check=full
to get all the juicy info.
π§ 3. GDB (The Stoic Therapist)
- Not flashy, but powerful. If you need to pause execution mid-Dijkstra and stare at your pointers like a disappointed parent:
g++ -g your_code.cpp -o output
gdb ./output
Then in GDB:
run
bt # Backtrace on crash
info locals
- Common Commands:
- run β start execution
- break main β set breakpoint
- next, step β step through code
- print var, info locals β inspect variables
- bt β backtrace after crash
π 4. Godbolt (Optional, but Very Nerdy)
- Not for runtime bugs, but if you ever want to see how your C++ compiles to assembly, or compare optimizations. Itβs fun for algorithm analysis.
π 5. Clang-Tidy
-
For static analysis. Lints C++ code and catches suspicious patterns before they explode.
-
Run like:
clang-tidy your_code.cpp -- -std=c++17
Bonus: Compilation Flags
- Always use
-Wall -Wextra -pedantic
for extra compiler warnings.