Menu
Debugging C++ in Visual Studio

Debugging C++ in Visual Studio

Debugging C++ in Visual Studio - ▶️ Playlist


Core Concepts

1. Breakpoints (2:36)

  • Purpose: Pause program execution at specific lines to inspect state.
  • Setup:
    • Click left gutter OR press F9 on the desired line.
      int main() {  
        int a = 8; // ← Set breakpoint here  
        a++;  
      }  
      
  • Run with Debugger: F5 to start debugging.

2. Stepping Through Code (6:28)

| Action | Shortcut | Behavior |
|——————-|————–|———————————————–|
| Step Into | F11 | Enter function calls (e.g., Log()). |
| Step Over | F10 | Execute current line without entering functions.|
| Step Out | Shift+F11 | Exit current function and return to caller. |

Example Workflow:

  1. Breakpoint hits at int a = 8;
  2. F10 to execute line → a becomes 8.
  3. F10 again → a++ increments to 9.

3. Memory Inspection (9:55)

  • View Variables:
    • Autos/Locals Window: Auto-displays variables in scope.
    • Watch Window: Manually track variables (e.g., a, &a for address).
      const char* string = "Hello"; // Watch: `string` shows address, `*string` shows 'H'  
      
  • Memory View:
    1. Debug → Windows → Memory → Memory 1.
    2. Enter variable address (e.g., &a) to view raw hex/ASCII data.

Debugger Windows

1. Autos/Locals

  • Autos: Shows variables relevant to the current line.
  • Locals: Lists all variables in the current scope.

2. Watch

  • Track custom variables/expressions:
    a       // Value of `a`  
    &a      // Memory address of `a`  
    *string // First character of `string`  
    

3. Memory

  • View raw program memory: Enable Memory View Output

  • Memory View Example: Memory View Example
  • Hex Format: CC = uninitialized stack memory (debug mode only).

Common Debugging Scenarios

1. Uninitialized Variables (10:40)

  • Symptom: Garbage values (e.g., a = -858993460 in debug mode).
  • Fix: Initialize variables before use.
    int a = 0; // ✅ Good  
    int b;     // ❌ Bad (contains CC bytes)  
    

2. Memory Corruption (14:12)

  • Sign: Unexpected values in memory/watch window.
  • Debug:
    1. Set breakpoint before/after suspected code.
    2. Compare memory states.

Pro Tips

  1. Always Use Debug Mode:
    • Compiler adds safety checks (e.g., CC filler for uninitialized memory).
    • Release mode optimizations may break breakpoints.
  2. Hex Display:
    • Right-click Watch window → Hexadecimal Display for raw values.
  3. Conditional Breakpoints:
    • Right-click breakpoint → Conditions (e.g., break when a > 5).

Video Reference Cheat Sheet

Key Principle:
The debugger lets you freeze time to inspect variables/memory. Combine breakpoints and stepping to isolate bugs efficiently. 🐞🔍