CSCI 201 Lab 8 -- Debugging in C++

Getting ready

If a directory called C:\files\Lab8 presently exists on your computer, delete it.

We're going to give you 2 Visual C++ project folders (i.e., workspaces) to start with this time. First download a ZIP'ed copy of all Lab 8 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\Lab08 directory with two subdirectories: weekdays and buggy.

Open the weekday workspace and make sure it compiles. We will uses this workspace to learn about the debugger.

Using the Visual C++ Debugger

This Lab will familiarize you with Microsoft Visual C++'s Debugger. The debugger provides two powerful features which this lab will demonstrate.
  1. Executes your program line by line, instead of at full speed.

  2. Allows for examination of any variable at any point in your program.

Starting a Simple Trace

Start the debugger, by placing the "blinking cursor" on the top line of your code and choose Run to cursor (Ctrl+F10) from the Start Debug submenu of the Build menu (or floating toolbar).

This should bring up a code window with a yellow arrow placed after the first executable instruction. The yellow arrow to the left of a statement means that execution is suspended at this line. This will be the next statement executed.

Click somewhere lower in the code and again choose Run to cursor (Ctrl+F10) from the Debug menu (or floating toolbar). This will cause the program to run until it reaches the line with the code you clicked on. Use this feature when you want the debugger to jump to a specific line of code.

Continue the Trace

Step Over -- instruction by instruction stepping excluding functions: Step Into -- stepping inside functions:

NOTE: As well as Stepping Into programmer-defined function, the debugger also allows for stepping into C++ library functions.

Stop the Trace

To stop the debugger executing your program:

Variable Examination

When in debug mode, the bottom portion of the Visual C++ window is divided into two windows--the "Variables" window on the left side and the "Watch" window on the right side.

Examination Methods

Lab Checkoff 1

In the watch window, type the names of a couple of variables and demonstrate that the values of these variables change as you step through the program.

Importance of Breakpoints

Breakpoints are markers that are placed at specified lines of code that cause the debugger to suspend execution of the program upon reaching that line of code. Breakpoints are important because they give the programmer a means to stop at a particular line of code and verify that a program is executing correctly.

Setting Unconditional Breakpoints

Start Debugging Process

Conditional Breakpoints

NOTE: Breakpoints are persistent. When a project is closed and reopened, any breakpoints set during a debugging session will still be set.

Lab Checkoff 2

Demonstrate the execution of your conditional breakpoint.

Expression Evaluation

Quickwatch

Activating Quickwatch

Lab Checkoff 3

Demonstrate the execution of your quick watch selection.

Fixing a program

Open the workspace c:\files\lab08\buggy\buggy.dsw. The program in this project has several problems. 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 prime numbers between the first integer and the second integer. For example, if you enter 3 5, it should print out
3
5
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 probably be infinite loops in your future programs.) Now use your knowledge of the debugger to find why this program doesn't work and fix it. There will be several things that you need to fix.

Lab Checkoff 4

Show the instructor the corrected program.