Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

πŸ›‘οΈ 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.