C/C++-SPECIFIC GUIDANCE:
- Use C/C++ extension for VS Code (by Microsoft)
- Ensure GDB or LLDB debugger is installed
- Compile with debug symbols (
-gflag) - Set breakpoints in
.c,.cpp,.cc,.h,.hppfiles - Use 'cppdbg' debug configuration type
- Compilation: Always compile with
-gflag and disable optimizations (-O0) for debugging - Memory Issues: Watch for buffer overflows, memory leaks, and dangling pointers
- Pointers: Carefully inspect pointer values and dereferenced contents
- Stack Frames: Use call stack to trace function calls and local variables
- Core Dumps: Enable core dumps for post-mortem debugging of crashes
{
"type": "cppdbg",
"request": "launch",
"name": "Debug with GDB",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}{
"type": "cppdbg",
"request": "launch",
"name": "Debug with LLDB",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}- Use
printf()orstd::coutfor quick debugging - Watch for uninitialized variables
- Check array bounds carefully
- Be aware of undefined behavior from pointer arithmetic
- Use address sanitizer (
-fsanitize=address) to detect memory errors - Use valgrind for memory leak detection (Linux)
- "Unable to start debugging": Ensure executable is compiled with debug symbols
- "No symbol table": Recompile with
-gflag - Breakpoints grayed out: Source file doesn't match compiled binary - rebuild
- Segmentation fault: Use backtrace to find the crashing line, check pointer operations
- Optimized away variables: Compile with
-O0to disable optimizations
- Valgrind:
valgrind --leak-check=full ./program - Address Sanitizer: Compile with
-fsanitize=address -fno-omit-frame-pointer - Watch expressions: Monitor pointer values and array indices during stepping