Debugging Techniques in C++

Debugging is the process of finding and fixing errors (bugs) in your program. Good debugging skills save hours of frustration and help you become a better programmer.

We will cover:

  1. General Debugging Techniques (suitable for all levels)
  2. Using IDE Tools (the main focus – step-by-step with popular IDEs)
  3. Advanced Debugging Techniques (for BS-level students)

General Debugging Techniques (For All Students)

These work even without fancy tools — great for beginners (Class 5–10).

 
 
TechniqueWhat You DoBest ForExample Use Case
Print Statements (cout)Add cout << “Value of x = ” << x << endl;Logical & Runtime errorsSee variable values in loops
Commenting CodeTemporarily disable parts with // or /* */Isolate the bugCheck if error disappears after commenting
Rubber Duck DebuggingExplain your code line-by-line to a toy/ friendLogical errorsForces you to think clearly
Dry Run (Paper Tracing)Run code on paper with sample inputLogical & off-by-one errorsTrace loops and conditions manually
Test with Simple InputsUse small, easy numbers (1, 0, -1)All types of errorsTest student record system with 1 student
Divide and ConquerTest small parts of code separatelyLarge programsTest addStudent() function alone
 

Example: Print Debugging in Student Record System

C++
 
void addStudent() {
    cout << "Starting addStudent..." << endl;
    Student s;
    cout << "Enter ID: "; cin >> s.id;
    cout << "ID entered: " << s.id << endl;  // Debug print
    // ... rest of code
}
 
 

Using IDE Tools – Step-by-Step (Main Focus)

Modern IDEs make debugging 10× faster. We will cover the most popular free IDEs used by students in India and worldwide.

A. Visual Studio Code (VS Code) – Most Popular for Students

Best for: Class 8 to BS level Free, lightweight, works on Windows/Mac/Linux

Step-by-Step Debugging in VS Code

  1. Install extensions:
    • C/C++ by Microsoft
    • Code Runner (optional)
  2. Open your project folder (e.g., folder containing student.cpp).
  3. Create or open tasks.json and launch.json:
    • Press Ctrl+Shift+P → “Debug: Add Configuration” → Select “C++ (GDB/LLDB)”
  4. Set Breakpoints:
    • Click in the gutter (left of line numbers) → red dot appears.
    • Example: Click on line cout << s.id; in addStudent().
  5. Start Debugging:
    • Press F5 or click green play button → “Run and Debug”.
    • Choose “(gdb) Launch” if asked.
  6. What You See When Debugging Starts:
    • Program pauses at breakpoint.
    • Variables panel shows current values (hover also works).
    • Call Stack shows function call sequence.
    • Debug toolbar: Continue (F5), Step Over (F10), Step Into (F11), Step Out (Shift+F11).
  7. Useful Shortcuts:
    • F5 → Continue
    • F10 → Step Over (skip function calls)
    • F11 → Step Into (enter function)
    • Shift+F11 → Step Out (exit current function)
    • Ctrl+Shift+F5 → Restart
    • F9 → Toggle breakpoint
  8. Watch Variables:
    • Right-click variable → “Add to Watch”
    • See value change live.
  9. Example: Debugging Student Record System
    • Set breakpoint inside while (inFile.read(…))
    • Run → Program pauses → Check s.id, s.name values
    • Step through each record

B. Code::Blocks – Very Student-Friendly

Best for: Class 5 to 12 Comes with compiler pre-installed (MinGW)

Debugging Steps

  1. Open your project or single file.
  2. Click on line number → red breakpoint dot appears.
  3. Press F9 (or Debug → Start/Continue) to run in debug mode.
  4. When program pauses:
    • Watch window shows variables.
    • Hover mouse over variable to see value.
    • Use toolbar: Next line (F7), Step into (F8), Continue (F9).
  5. Memory view, CPU registers also available.

C. Dev-C++ (Old but still used in many schools)

  1. Press F9 to set breakpoint.
  2. Press F8 to debug.
  3. Use “Debug” menu → “Step Into”, “Step Over”, “Continue”.

D. Visual Studio Community (Windows Only)

Best for: BS level, professional look

  1. Set breakpoint (F9).
  2. Press F5 to start debugging.
  3. Autos, Locals, Watch windows show variables.
  4. Immediate window to run code live.
  5. Exception settings to catch runtime errors automatically.

Quick Comparison Table for Your Website

 
 
IDEEase for BeginnersBreakpointsVariable WatchStep Into/OverFree?Recommended For
VS CodeMediumYesYesYesYesClass 8–BS
Code::BlocksVery EasyYesYesYesYesClass 5–12
Dev-C++EasyYesBasicYesYesClass 6–10
Visual StudioMediumYesAdvancedYesYesBS & Projects
 

Advanced Debugging Techniques (BS Level)

 
 
TechniqueDescriptionTool/IDE Support
Conditional BreakpointsBreak only when condition is true (e.g., id == 101)VS Code, Visual Studio, Code::Blocks
Watch ExpressionsMonitor expressions like total = a + bAll major IDEs
Memory DebuggingFind array overflows, memory leaksValgrind (Linux), AddressSanitizer (g++)
Exception CatchingPause when exception occurs (e.g., divide by zero)Visual Studio, VS Code
Reverse DebuggingStep backwards in time (rare but powerful)gdb with recording
Logging FrameworksUse libraries like spdlog instead of coutAdvanced projects
Unit Testing + DebuggingWrite tests, debug failing test casesGoogle Test + IDE debugger
 

Example: Conditional Breakpoint in VS Code

  1. Right-click breakpoint → “Edit Breakpoint…”
  2. Enter condition: s.id == 5
  3. Program pauses only when student ID is 5 — perfect for large student records.

Using Compiler Sanitizers (BS Level)

Compile with:

Bash
 
g++ -g -fsanitize=address student.cpp -o student
 
 
  • Detects array out-of-bounds, use-after-free, etc.
  • Shows exact line of memory error.