Saturday, October 20, 2012

Program Debugging and Execution

The really nice feature of VC and Win32 console applications is its total runtime debugging support. You can simply click on the debug button Go (F5) to begin the execution of your program. If a runtime error occurs, the debugger will pop up at the point of the error with full debugging information available. If you place the mouse cursor on top of a variable in the code, the debugger will immediately show you the current contents of that field! The list of features goes on and on.

However, normally you do NOT want to just press the F5 key or use the Go button. Why? Yes, if there is a runtime error, the debugger pops back up at the offending instruction. But if there is no hard fail, when the program terminates, the debugger closes the DOS window and shuts down. Your output window disappears leaving you with no idea of whether or not it ran correctly. For simpler programs, press the F10 (Step Over) and or F11 (Step Into) key. These buttons execute one instruction of your program and then stop, allowing you to view the results of that single instruction or watch the paths taken when decisions are made. The difference between F10 and F11 is simple.

The difference is Step Over versus Step Into. When the Debugger gets to a function call either a system provided one, like cin >> quantity, or a user written function, Step Into goes on into the first instruction of that function so that the function itself can be debugged. Step Over simply executes the entire function and then returns control to you. You normally want to Step over system functions and Step into your own functions.  This approach is fine for smaller programs. But for larger programs with many functions it is impractical if not extremely time-consuming. There is another way, using breakpoints. The first action of a debug run is to set a break point. A break point is a flag placed on a line of code that tells the debugger to halt the execution of your program when and if it gets to that line of code. Technically, the line that has the break point set on it is the next instruction that would be executed if you continue to run the program. In other words, it has not yet been executed.

How do you set break points? Click on the line where you want the execution of your program interrupted so you can inspect things. Press either F9 or the button Insert/Remove Breakpoints. You should see a red dot appear to the left of that line of code. Press it again and the break point is removed. It acts like toggle switch — on — off — on — off.
Once you have set a break point, then go ahead and run the program with the Go button or F5 key. On which line of code should you set a break point? Ah! That depends on the errors you have in your program and what you want to see and so on. However, for sure, you should place a
break point on the last line of the main program, on the return 0; // terminate and return back to DOS Figure 5 shows the setting of just such a break point in a simple C++ program that is to print Hello 5 times and then quit.

BIO:
Brents Lyons is an IT professional from Exam key. Are you really Looking for this tk0-201 exam assistance? Move ahead to take the benefit of a00-212exam question and pass your exam easily.


No comments:

Post a Comment