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:
- General Debugging Techniques (suitable for all levels)
- Using IDE Tools (the main focus – step-by-step with popular IDEs)
- 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).
| Technique | What You Do | Best For | Example Use Case |
|---|---|---|---|
| Print Statements (cout) | Add cout << “Value of x = ” << x << endl; | Logical & Runtime errors | See variable values in loops |
| Commenting Code | Temporarily disable parts with // or /* */ | Isolate the bug | Check if error disappears after commenting |
| Rubber Duck Debugging | Explain your code line-by-line to a toy/ friend | Logical errors | Forces you to think clearly |
| Dry Run (Paper Tracing) | Run code on paper with sample input | Logical & off-by-one errors | Trace loops and conditions manually |
| Test with Simple Inputs | Use small, easy numbers (1, 0, -1) | All types of errors | Test student record system with 1 student |
| Divide and Conquer | Test small parts of code separately | Large programs | Test 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
- Install extensions:
- C/C++ by Microsoft
- Code Runner (optional)
- Open your project folder (e.g., folder containing student.cpp).
- Create or open tasks.json and launch.json:
- Press Ctrl+Shift+P → “Debug: Add Configuration” → Select “C++ (GDB/LLDB)”
- Set Breakpoints:
- Click in the gutter (left of line numbers) → red dot appears.
- Example: Click on line cout << s.id; in addStudent().
- Start Debugging:
- Press F5 or click green play button → “Run and Debug”.
- Choose “(gdb) Launch” if asked.
- 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).
- 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
- Watch Variables:
- Right-click variable → “Add to Watch”
- See value change live.
- 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
- Open your project or single file.
- Click on line number → red breakpoint dot appears.
- Press F9 (or Debug → Start/Continue) to run in debug mode.
- When program pauses:
- Watch window shows variables.
- Hover mouse over variable to see value.
- Use toolbar: Next line (F7), Step into (F8), Continue (F9).
- Memory view, CPU registers also available.
C. Dev-C++ (Old but still used in many schools)
- Press F9 to set breakpoint.
- Press F8 to debug.
- Use “Debug” menu → “Step Into”, “Step Over”, “Continue”.
D. Visual Studio Community (Windows Only)
Best for: BS level, professional look
- Set breakpoint (F9).
- Press F5 to start debugging.
- Autos, Locals, Watch windows show variables.
- Immediate window to run code live.
- Exception settings to catch runtime errors automatically.
Quick Comparison Table for Your Website
| IDE | Ease for Beginners | Breakpoints | Variable Watch | Step Into/Over | Free? | Recommended For |
|---|---|---|---|---|---|---|
| VS Code | Medium | Yes | Yes | Yes | Yes | Class 8–BS |
| Code::Blocks | Very Easy | Yes | Yes | Yes | Yes | Class 5–12 |
| Dev-C++ | Easy | Yes | Basic | Yes | Yes | Class 6–10 |
| Visual Studio | Medium | Yes | Advanced | Yes | Yes | BS & Projects |
Advanced Debugging Techniques (BS Level)
| Technique | Description | Tool/IDE Support |
|---|---|---|
| Conditional Breakpoints | Break only when condition is true (e.g., id == 101) | VS Code, Visual Studio, Code::Blocks |
| Watch Expressions | Monitor expressions like total = a + b | All major IDEs |
| Memory Debugging | Find array overflows, memory leaks | Valgrind (Linux), AddressSanitizer (g++) |
| Exception Catching | Pause when exception occurs (e.g., divide by zero) | Visual Studio, VS Code |
| Reverse Debugging | Step backwards in time (rare but powerful) | gdb with recording |
| Logging Frameworks | Use libraries like spdlog instead of cout | Advanced projects |
| Unit Testing + Debugging | Write tests, debug failing test cases | Google Test + IDE debugger |
Example: Conditional Breakpoint in VS Code
- Right-click breakpoint → “Edit Breakpoint…”
- Enter condition: s.id == 5
- 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.