|
Introduction to The Microsoft Visual C++ Debugger What is the Debugger? The debugger is a Microsoft Visual C++ IDE tool that helps the programmer to find logical errors in programs that compile, link, and run successfully but do not produce expected results. With the debugger, you can step through your program one line at a time. You can trace variables to watch the variable values change, even if your program includes no output statements for those variables. You can insert breakpoints (points in your program where the program will stop) so you can inspect the state of the program at those points. Before you can use the debugger on a program, you must first successfully compile the program in the usual way. How to step through your program? To step through your program, choose start Debug on the Build menu and then choose Step Into on the resulting submenus. An arrow will appear in the panel that contains your program. The arrow points to the line to be executed next. Once you start the debugger, you will have a new menu called Debug at the top of the Visual C++ environment display. Every time you choose Step Into on the Debug menu, one more line of your program is executed and the arrow moves down one line. Whenever you are through with using the debugger on your program, then you should choose Stop Debugging on the Debug menu Step Into versus Step Over The Debug menu has Step Into as well as Step Over command. The two commands are similar, but do have an important difference. With the Step Over command, a method invocation is considered a single command and the arrow will move to the next statement. On the other hand, if you instead choose the Step Into command, then the method invocation will begin and the body of the method definition will be displayed. Stepping into cin and cout The Step Into command can step into the code for cin and cout or other predefined function operator. If a window appears with strange-looking code, that is likely to be what has happened. To get out of this code and back to your program, choose Step Out from the Debug menu. (You may have to choose Step Out several times and may need to cancel a dialogue box that appears asking you for the location of something, but you will eventually return to your program. You can also just end the debugging session and start over. To avoid this problem, use Step Over, rather than Step Into, when the arrow is on a line with cin, cout, or predefined function invocation. Use Quick Watch Press F11 from the keyboard to set a quick watch. Choosing Quick Watch on the Debug menu will produce a dialog box that allows you to enter an expression. You can enter any arithmetic expression involving the variables in your program, even if your expression does not occur in your program. If you then click that Add Watch button in the dialog box, then the dialogue box will go away, and the added expression, as well as the value of that expression, will appear in the panel at the bottom of the Visual C++ display Breakpoints Suppose you want to run your program up to a certain line of your program and then stop the program at that line, perhaps because you want to check what the variable values will be when your program reaches that line. Simply place the cursor at the line where you want the program to stop and select Run to Cursor on the Debug menu. The program will then execute every thing up to the line with the cursor. f you want, you can then reposition the cursor then choose Run to Cursor again to run your program up to some later line. Suppose you do not want to be continually moving the cursor, but still you want to run your program up to a certain line and stop at that line. In this case, you can set a breakpoint at the line. A breakpoint is a marked line in your code, when you are running your program with the debugger, the Go command on the Debug menu will run your program up to the next breakpoint. To restart your program from where it was left off, again choose Go command from the Debug menu. Note if you have a breakpoint in the body of a loop, then the Go command will run your program up to that line again and again until the loop is finished. Now you need to know how to set a breakpoint. To do this you need to have the Build toolbar or the Build minibar available as follows: First be sure you are running the debugger, then choose Customize on the tools menu. Choose the toolbars tab and click the box next to the word Build (To make the Build minibar visible, click on the Build MiniBar box) You can then close the Customize dialog box. The Build toolbar should then be visible. Once the Build toolbar is visible, it is easy to set breakpoints. To set breakpoint, simply move the cursor to the line on which you want the breakpoint, click your mouse so the cursor stays on that line, and then click the hand icon indicating the that the line is marked as a breakpoint. You can set as many breakpoints as you want. To remove a breakpoint, simply do the same thing: move the cursor to line with the breakpoint and click the hand on the Build toolbar (or the Build minibar). The breakpoint will then go away. |
|
|