Debugger Tips

This page collects various tips about the Visual C++ Debugger.

  1. What is a debugger?
  2. How to start the debugger
  3. How to stop the debugger
  4. Use the variable window
  5. Use the watch window
  6. Removing a variable from the watch window
  7. Displaying expressions
  8. Breakpoints
  9. Step Over/Into/Out
  10. Contexts
  11. Displaying chars and strings
  12. Dereferencing pointers
  13. Displaying arrays and structures
  14. Getting the debugger to work in MSVC 5.0

 


 

  1. What is a debugger? A debugger is a tool which helps a programmer find errors ("bugs") in a program. You can make the program stop when it reaches a particular statement: this is called a "breakpoint." While at a breakpoint, you can examine the values of your variables and investigate the state of your program. Your C program is visible in a window at this time. Then you can continue executing from the breakpoint, perhaps to another breakpoint. Along the way you can set "watches" to view the values of variables you are interested in.
  2. How to start the debugger: The Visual C++ debugger is built-in to the Visual C++ development environment. You can start the debugger by selecting "Build/Debug/Go". Alternatively you can press F5 to start the debugger. Before starting the debugger, you probably should set a breakpoint, or your program will simply run to completion as usual.
  3. How to stop the debugger: During the debugging process, you can stop it by selecting "Build/Stop Debugging". Alternatively you can press "Alt+F5" to stop debugging.
  4. Use the variable window: Variable window will display variable values local to a function. Selecting "View/Variables" will pop up the watch window. There are three tabs in the lower left corner of the variable window. You can click on different tabs to display different information:
  5. Use the watch window: Watch window displays information about the variables that you are interested in. Selecting "View/Watch" will pop up the watch window. You can enter variable names in the "Name" column of the watch window, the debugger fills in the Type and Value columns with the corresponding information.
  6. Removing a variable from the watch window:
  7. Displaying expressions: You can display more than just simple variables in the watch window. You can enter expressions there, and their values will be displayed. This can be useful, for example, in displaying the values of logical expressions controlling if statements and loops.
  8. Breakpoints: If you want to stop the program execution at a particular line, use the mouse to click on that line and then press F9. When you see a red circle showing up next to that line, you have successfully put a breakpoint there. Pressing F9 again removes the breakpoint. You cannot put a breakpoint at a variable declaration line. The breakpoint is not actually activated until the next time the program reaches that statement, if the program is running in debug mode.
  9. Step Over/Into/Out: When single-stepping a program, and the next statement to be executed is a function call, "Debug/ Step Over" will execute the entire function, whereas "Debg/Step Into" will step you into the function. Use "Step Into" for your own not-quite-debugged functions, and "Step over" for system functions like printf, or debugged functions of your own. To run a function until its end and then return to the caller, click "Debug/Step Out." If you have stepped to the end of your program, you can click "Debug/Go" to finish execution. The short-cuts for doing "Step Into","Step Over" and "Step Out" are listed under the "Debug" menu. For example, F8 can be used for "Step Into".
  10. Contexts: Visual C++ will only display information for global variables and local variables which are in the the context of execution. If you see something like score CX0069:Error , it means either the variable is out of context or is not defined. For example, if you have the following program:
    #include <stdio.h>
    int param = 100;

    void main(void)
    {
    int param;

    param = 300;
    }
    To display the value of the global variable param, you need to enter ::param in the watch window. To display the variable param in function main, you just need to enter param in the watch window.
  11. Displaying chars and strings: If you declare a variable of type char or char *, Visual C++ will display the variable value as well as the charater or character string it points to in the "Value" column.
  12. Dereferencing pointers: If you have a pointer variable in the watch window, there will be a small button next to the variable name. By clicking on the button, you can see the memory location it points to in the "Value" column.
  13. Displaying arrays and structures: To display the contents of an array or struct, enter the variable name into the watch window as usual. You will see a small button appears next to the variable name. By clicking on the button, you can expand or contract your view of the variable. The button displays a plus sign (+) when the variable is displayed in contracted-form, and a minus sign when it is displayed in expanded form.
  14. Getting the debugger to work in MSVC 5.0 Several people have reported difficulty in getting debugging to work under 5.0. There are several things you can do: