Debugging C++ with Visual Studio!

What is a bug anyway? Long ago, a lady named Grace hopper found a moth in a relay in the Eniac computer and exclaimed “I have a bug in my program”! To this day, when our programs do not work the way we expect, we say we have a bug in the code. Debugging is getting rid of those and getting the code to work!

So how do we do this?

Visual Studio Debugging

Most modern IDE systems provide tools you can use to work your way through the code one line at a time. We can run through a series of statements, stop the program and step it one line at a time. As we do this, we can inspect the value currently stored in our variables.

This is a powerful way to find problems in your code!

Breakpoints

We can mark a place in our code where we want the computer to stop processing and let us see what is going on. We do this by setting a breakpoint in our code at some point. We then let the program run until it hits this line (if ever). Once that happens, the IDE will stop the program and let you poke around.

Let’s see how this works!

Fire up Visual Studio and start a new project. Name this project DebugDemo and store it in a location of your choosing (I used c:\GSP115/DebugDemo).

Choose Win32 Console Application for the type of project.

First, set up this simple chunk of code in VS:

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    double var1 = 0.0;
    for(int i=0; i<10; i++){
            var1 = var1 + 2.0;
    }
    cout << var1 << endl;
    return 0;
}

Now, this code does nothing much (what?). We will use it to try debugging.

Start up the debugger

Click on Build --> Build Solution and make sure you have no errors.

Next, right-click on the line where the variable is modified (var1 = var1 + 2.0). Set a breakpoint there. You should see a red dot nect to the line.

Now, click on Debug --> Start Debugging and see what happens. Your program should start, then ryn up to the breakpoint, then stop. You now hate the ability to poke around. Look at the bottom left panel. Do you see the variable named var1? It has a value of zero since we have not executed the line where the breakpoint is set yet.

Move the mouse over the menu at the top that looks like this:

../../_images/DebugMenu.png

You are looking for the menu item that says Step into. Click on this and see what happens.

With any luck, the value of the variable should change to 2.0!

The menu selections you will use for debugging are these:

  • Step into - execute this statement. If it is a function call, move into the function and continue.
  • Step over - execute this statement. If it is a function call, stop when the function returns (on the next statement after the call).
  • Step out - If you find yourself inside a function and want to get out of it. choose this to let it run and go back to the caller. This is handy if you step into a function by mistake!

Click on the triangle button at the far left. This one (Continue) runs the program until it hits another breakpoint. Since we are in a loop, you will stop on each pass through the loop, and you can see the variable increase by two each pass.

This gives you a start on running the debugger! Play with it to see what it can do!

Table Of Contents

Previous topic

C++ Functions (revisited)

Next topic

Week 3 Objectives

This Page