CSCI 201 Lab 7 -- Debugging in Visual C++

This week you try out the Visual C++ debugger.

Getting ready

First of all, if a directory called C:\files\lab07 presently exists on your computer, delete it. Leave the Explorer window open with c:\files contents displayed.

We're going to give you some programs and workspaces to start with this time. First download a ZIP'ed copy of the Lab 7 workspaces and store it in the C:\files directory.

Use Powerzip to extract the files in it to the c:\files directory.

This should create a C:\files\lab07 directory and four subdirectories, each with its own project, within the C:\files\lab07 directory. Check the window for c:\files where you should see a new lab07 directory. If you don't see it, make sure you extracted the files to the c:\files directory.

Initial debugging

Start up Visual C++ and open, via the File » Open Workspace menu choices, the workspace c:\files\lab07\Zero\Zero.dsw. Build the program.

Now we are going to use the debugger to step through your program. Warning: Debuggers are not easy to master, but you'll save yourself a great deal of time if you learn to use them.

Start the debugger by pressing the F11 key. You will see an new window with debugger buttons on top of your other windows.

Debug window

You may have to move the debugger window over to the right to see the window that contains your program. That window now has a small yellow arrow pointing to the line of code that will be executed next.
Initial debug window

Now press F10 to single step through your code. Notice the yellow arrow move as you tiptoe through the C code. You should also see a window displaying the values of objects that are variables within your program. Hit the Local tab in this window to set it to display the variable values you need for this lab.
Variable display window

It's best to stop when you get to the closing curly brace.
End of the line

You should stop the debugger with the menu selections Debug » Stop Debugging or the shift-F5 key combination. However, there's a good chance that you'll someday you'll go to far and find yourself debugging some Intel assembly code. If you see something like "add esp,0Ch", you should immediately shift-F5 to stop the debugger.

Now set the value assigned to x in its declaration to 1, rather than 0. Now step through the program and see if you execute the other branch of the if-then-else.

What? You didn't. Well, there's a bug in your program. Fix it. Hint: what does the assignment operator, '=', do?

Instructor check-off 1

Show your instructor the fixed code.

Debugging with I/O

Go through the file menu File » Close Workspace to close the Zero workspace. Now open the workspace c:\files\lab07\Nand\Nand.dsw and study the program.

Start debugging with F11 key and then use F10 to step through the code. The interesting twist occurs when you get to the input statement:

No matter how much you F10, the yellow arrow won't move. You need to find the console window for your program and give it the input it wants. ALT-TAB might reveal the console window. If that doesn't work, look for the application called simply Nand at the bottom of your screen.

If you want your input characters echoed as you type them, you must type the F10 after the arrow reaches the input statement. So use F10 to step to the cin line and then use it again on the line. Then go to the console window and enter the characters. If you go back to the console while the arrow is sitting on the input statement without pressing F10, you will not see the characters you type.

Continue to step through the program until you reach the end.

Instructor check-off 2

Show you instructor that you managed to reach the return statement while debugging the program.

Setting a breakpoint

Close the Nand workspace and open the workspace c:\files\lab07\Nested.dsw Again look over the program.

Use your mouse to move the cursor to the program statement

Now start to add a breakpoint with the file menu selections Edit » Breakpoints. This will display a dialog box.
Breakpoint dialog
Press on the right-pointing arrow to the right side of the empty text field below the words Break at. You should then be given a choice between a line number, like 13, and Advanced. Go for the line number and then click OK.

A red dot should appear just to the left of the line you chose. This red dot shows you where a breakpoint has been placed in your code. When you run your program, execution will stop when the code at the breakpoint is reached. The debugger window will be displayed, and you will be able to debug you code starting at the breakpoint.
Breakpoint dialog

Now press the F5 key or use the menu choices Build » Start Debug » Go to begin execution of your program. Your code should be executed up to the breakpoint you just set. At that time, you should see the yellow arrow on top of the red dot. At that point, you can continue to use F10 to single step or you can return to warpspeed with F5. You should examine the variables in the window at the bottom left of you screen to confirm that the values you entered have been stored in p, q, and r, before continuing with either single steps (F10) or regular running (F5).

Instructor check-off 3

Set two more breakpoints within the if - then - else part of the program, one at the first else and the other at the second else. Run the program using F5 to stop at each breakpoint. Try different inputs. Does the program always stop all all the breakpoints? Why not? Show your instructor what the program does and explain why it may skip some of the breakpoints. Then exit the debugger and close this workspace.

Fixing a program

Open the workspace for c:\files\lab07\Overlap\Overlap.dsw. This project has a typical loop problem. This loop in this program is a for loop, which is just a shorthand way of writing a while loop. Remember that the for loop:


is exactly the same as the while loop

i = 1;

Build this program and run it. It should first ask you to enter two integers, the second one larger than the first, and then it should print out all the integers from the first integer to the second integer. If you enter 3 5, it should print out
Instead, it gets stuck in an infinite loop. Hit Control-C (hold down the ctrl key and press the C key) to break out of the program. Remember that key combination - there will be infinite loops in your future programs! Now lets use the debugger to find why this program doesn't work.

Hit F11 to start the debugger. Oh, Oh - it wants the source code for the main function in ezwin and we don't have it! Hit cancel and now you'll see a screen full of assembly language! Better not try to step through that! Hit shift-F5 to get out of the debugger. So how do we get past main() to debug our code in ApiMain()?

Remember breakpoints? We need something like a breakpoint to let you run your program till it gets past the ezwin stuff and into ApiMain. Move the cursor to the start of the for loop in ApiMain. Now hit Ctrl-F10; this is the run to cursor command. Now the the programs runs to the the point where you positioned the cursor and stops in the debugger. Now you'll be able to single step through the loop to see why it never ends. Hint: look at the value of the loop counter variable, i, in the debugger's variable window. You'll have to click on the scroll bar on this small window so that you can move it down to where it shows i. Notice that you can see the value of any variable by simply placing your mouse cursor on it, but the value you see that way is the value when you put the cursor on the variable. The value you see in the variable window is always the current value stored in a variable.

OK, i isn't changing the way we think it should. It should start at the value stored l and increment by one each time you step through the loop. Look at the code and watch the value of i as you step through the code to figure out what is happening and why. Once you understand what the program is doing, you have a good shot at figuring out how to make it do what it is supposed to do. If you don't understand EXACTLY what a program is doing, you have very little chance of modifying it so that it will do what you want it to do. Figuring out what your program is doing is what debuggers are all about!

Now how do we fix it? To really fix this problem, we have to fix the code and then rebuild the executable, but we can get out of the infinite loop in order to what the rest of the program does by giving i a value at least as big as m. Place the mouse cursor on m to find its value. Now go to the variable window and double click on the value shown for i. Enter a new value at least as big as m. The debugger lets you singlestep, set breakpoints, see the values in your variables, and even change those values. It also does a bunch of other stuff. This is a great way to find the errors in your code!

If you give i a big value, then when you single step a few times you should leave the loop. If you step off the bottom of the program you'll find yourself back in the ezwin assembly language code again. Hit shift-F5 to get out of the debugger. Fix the program.

Instructor check-off 4

Use the debugger to run the fixed program for your instructor.
Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page