Sharing the basic means of debugging C language programs using VC6.0

Source: Internet
Author: User
Tags assert cos variables printf
This article mainly introduces the use of VC6.0 to develop C language programs when debugging code some of the tips, need friends can refer to the next

(1) Set a fixed or temporary breakpoint

A breakpoint is a line in a specified program that is paused after the program runs to that line, allowing the programmer to observe what is happening during the profiling process. These situations generally include:

① in the variable window (varibles) to observe the current value of the variable in the program. The programmer observes these values in contrast to the expected value, and if it is inconsistent with the expected value, then the program running before this breakpoint must have a problem somewhere, to narrow down the fault area. For example, the following program calculates cos (x) and shows that the runtime finds that no matter how much x is entered, the result is 0.046414.

Copy Code code as follows:


#include <stdio.h>


#include <math.h>

void Main ()
{
int x;

printf ("Please input x:");
scanf ("% d", &x);
printf ("cos (x) =%fn", cos (x));
}

In this program, if you do not see the problem-the program is longer, more complex, it is difficult to see the problem, you should use debugging methods to locate the fault location.

② observes the value of a specified variable or expression in the Monitoring window (Watch). when there are many variables, using the Varibles window may be inconvenient, using the Watch window can be a purposeful, systematic observation of the changes in key variables.

③ the Output window to see if the current output of the program is the same as expected. Similarly, if you do not agree, the program that runs before this breakpoint must have a problem somewhere.

④ in the Memory window (Memory) to observe the changes in the data in memory. in this window, you can query and modify data directly from any address. For beginners, it is possible to understand more deeply how various variables, arrays, and structures occupy memory, as well as the process of array overstepping.

⑤ observes the nesting of function calls in the Call Stack window (called stack). This window is useful for parsing failures in the case of a complex function call relationship or a recursive invocation.

(2) Step execution procedure

Allow the program to be executed step by step (line) to observe whether the execution process conforms to the requirements of the pre. For example, the function expected by the following program is to read two numbers (x and Y) from the keyboard, to determine if x and Y are equal, to display x=y on the screen, and to display x<>y for equality. This is the feature that is required to implement, but the actual operation of the program is: no matter what input, will be displayed on the screen x=y and x& lt;>y, the program must have problems, but on the surface may not find the problem, using a single step, you can locate the point of failure, narrowing the scope of the view. For example, in the process of stepping, if you enter "2,3" and find that the values of x and Y do become 2 and 3, then "printf" ("X=yn") should not be executed at this point, but a step-by-trace is found to be executed, so most of the problems are in "if" (x = y).

Copy Code code as follows:


#include &lt;stdio.h&gt;


void Main ()


{


int x, y;

printf ("Please input x, y:");
scanf ("%d,%d", &x, &y);
if (x = y)
{
printf ("X=yn");
}
Else
{
printf ("X<>yn");
}
}

In the process of single step execution, we should use the method of stepping over, steps into, stage out and Run to cursor flexibly to improve the efficiency of debugging. It is recommended that in the process of debugging, remember and use the "step over, steps into, steps out, Run to Cursor" and other menu items shortcut keys, the beginning may be unfamiliar, slow operation, but adhere to a period of time can be born skillful, efficiency improvement.

(3) using assertions

an assertion is an examination of a hypothetical condition (understandable if the condition is set up without action, otherwise it should be reported), it can quickly discover and locate software problems, and automatically alarm system errors. The assertion can locate the problem which is very deep in the system and is difficult to find by other means, so as to shorten the location time of the software problem and improve the testability of the system. In practical application, the assertion can be designed flexibly according to the specific circumstances.
When using assertions, you must add the following at the beginning of the program:

#include <assert.h>

① can confirm the parameters of a function by using assertions. Example: If you have a pointer in a function parameter, you can check it before using the pointer to prevent others from calling this function using null pointers as arguments. The code is as follows:

Copy Code code as follows:


int exam_fun (unsigned char *str)


{


assert (str!= NULL); Assertion "Pointer is not NULL", if "null" (assertion does not set up) error


...//other program code


}

② can be used to confirm that something should not have happened. Example: The following program segment runs wrong, it's difficult to check and it's been a long time since I didn't know where there was a problem. Therefore, it is recommended to analyze the normal operation of the program what should be, whether the operation of the exception, for all (or key states) should be normal situation, the use of assertions, it is very likely to find the cause of the exception, and debugging efficiency is high. For this segment, we assert that the value of the variable I should be "i>=0 && i<size" and more critical, but whether it is possible to inadvertently modify (for example, other variables out of bounds) in the course of the run, you can use assertions to check if this happens.

Copy Code code as follows:


for (i=0 i&lt;size; i++)


{


...//other program code


assert (i&gt;=0 &amp;&amp; i&lt;size); Assertion "I's normal range of values", if the assertion does not set up the error


Array[i] = i;


...//other program code


}

Assertion is not established (an exception), the system will immediately error, at this point can enter the program debugging state, check the operation of the program.

(4) Operation menu related to debugging: Build Menu

Compile: Shortcut key Ctrl+f7. Compiles the source program file that is currently in the source window to check for syntax errors or warnings and, if so, displays in the Output window.
Build: Shortcut key F7. A connection to the file in the current project, if any, will also appear in the Output window.
Execute: Shortcut key Ctrl+f5. Run (execute) an executable program (file) that has been compiled and connected successfully.
Start Debug: Selecting this item will pop up a submenu with several options for starting the debugger to run. For example, the Go option is used to start a program from the current statement until a breakpoint is encountered or a program is terminated, and the step into option starts stepping through the program and then steps into the function when it encounters a function call. Run to The cursor option causes the program to suspend execution when it runs to the current mouse cursor (note that, before using this option, you must first set the mouse cursor to the line of the program you want to suspend). When you execute the selection of the menu, the debugger starts, and the Debug menu (instead of the Build menu) appears in the menu bar.

(5) Debugging-related Operation menu: Debug Menu

The Debug menu does not appear until the debugger is started (instead of the Build menu).
Go: Shortcut key F5. Start the program from the current statement until the breakpoint is encountered or the program is terminated (the same functionality as the Build→start debug→go option).
Restart: Shortcut key Ctrl+shift+f5. Start debugging the program from the beginning (this is often necessary when you make some changes to the program!). )。 When selected, the system will reload the program to memory and discard the current value of all variables (and restart).
Stop Debugging: Shortcut key Shift+f5. Interrupts the current debugging process and returns to normal editing state (note that the debugger is automatically closed and the Build menu is reused to replace the Debug menu).
Step into: Shortcut key F11. Step through the program, and when you encounter the function call statement, go inside that function and step from scratch (the same functionality as the Build→start debug→step into option).
Step over: Shortcut key F10. Step into a program, but when it executes to a function call statement, it does not go inside that function, but instead executes the function directly after it is executed, followed by the statement following the function call statement.
Step out: Shortcut key shift+f11. In conjunction with "step in", when execution goes inside the function, after stepping through several steps, you can return from within the function (stopping at the next statement of the function call statement) If you find that you no longer need to step through.

Run to Cursor: Shortcut key CTRL+F10. Causes the program to suspend execution when it runs to the current mouse cursor (note that the mouse cursor is set to the line of the program you want to suspend before using this option). In fact, the equivalent of setting a temporary breakpoint is the same functionality as the Build→start debug→run to Cursor option.

Insert/remove Breakpoint: Shortcut key F9. This menu item does not appear on the Debug menu (on the context menu of the toolbar and the program document), listed here is for the convenience of everyone to master the means of program debugging, its function is to set or remove a fixed breakpoint-the program line has a circular black dot flag, indicating that the row has set a fixed breakpoint. In addition, there are alt+f9 (all breakpoints in the management program), CTRL+F9 (disable/enable current breakpoint) associated with a fixed breakpoint.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.