Maybe more than half of a developer's time is facing errors, so good debugging/The error checking method (Tool) can reduce the burden on our work and shorten the boring debugging process.
The debugging environment provided by the VC development environment is excellent. We can use a single-step operation to set breakpoints to locate the problem. However, this type of tracking is very time-consuming, so we need to adopt some policies to make it easier for us to discover and locate errors, fortunately, VC provides strong support in this regard. In this section, we will first look at how to use the breakpoint setting and the TRACE macro to output the running status.
InPress F9 in the VC development environment to set a breakpoint in the row where the cursor is located, and then press it again to cancel the breakpoint. The significance of setting a breakpoint is that an interruption occurs when the line is run during the debugging process and is returned to the VC development environment. You can view the values of each variable in the development environment. The following is the code for testing. The row with a red circle above indicates that the row has a breakpoint:
Generally, you can set a breakpoint using the following techniques:
- It is set in the Code where the judgment is made, so that you can check whether the conditions on which the judgment depends are correct during running.
- Set the start of the function and check whether all the variables on which the function depends are set correctly.
- Set the end of the function and check whether the function changes the variable correctly.
- Before entering other functions/, Check whether the function is correct through the black box method.
- For a loop body, you should first test a condition with a small number of cycles to check whether the loop logic is correct, or set the breakpoint several times before the loop, and then cancel the breakpoint after running several times.
When you reach the breakpoint during debugging, you can use the context Variable Window (Variables) observe the value of the variable in the function. To observe global Variables or class member Variables that are not present in the function, add the variable name to the Watch window) enter the variable name. However, after the program is compiled, press the F5 key to execute the program in debug mode. When the program enters the breakpoint, the VC development environment will be automatically activated, and then we can observe the program running status. You can also add or delete breakpoints during debugging. For example:
If the value of the variable observed changes during running, the variable changes to red in the observation window.
The TRACE macro provided in MFC can help us conveniently output debugging information during program debugging and running. The TRACE macro is defined as TRACE (exp), where the expression uses the same expression as printf. For example, the following code:
Void CSam_sp_31Dlg: OnTest2 ()
{
Static int I = 5, j = 50;
Char szDeb [] = "debug string ";
TRACE ("trace I = % d j = % dstring = % s", I, j, s