Two methods of "dynamic debugging program": writing to new programmers

Source: Internet
Author: User

[Switch] Two Types of "dynamic debugging"ProgramMethod-writing to new programmers

Well, I thought it was good and I turned around. It's all basic stuff.

From: http://www.programfan.com/club/showpost.asp? Id = 147534 & t = o

Debugging a program is a tough task. Even experienced programmers are overwhelmed. ForCodeAfter looking for a few hours, we were not able to find any errors, and we were confused. Now we thought: Since the eyes didn't see any errors in the code, let's expose the error while running, and find the error in the code. This is the so-called "dynamic debugging ". Relatively, the program is not allowed to run, but a line of code reading, looking for errors, can be called "static debugging ".
Method 1: Use assertions.
C and C ++ have a very useful thing: assert (that is, assert), that is to say, Programmers think that what is in assert must be true. If the assertion fails (the programmer thinks it is inevitable, but in fact it is not true), it indicates that the program has a problem somewhere and the program will be terminated.
For example, if we want to calculate the integer r of the arithmetic square root of an integer x, if the calculation is correct, there must be r * r <= x, (R + 1) * (R + 1)> X, so we can write it like this:


# Include <assert. h>/* in C ++, use # include <cassert> */
Int squareroot (int x)
{
Int R;
/*
Computing R
*/
Assert (R * r <= X );
Assert (R + 1) * (R + 1)> X );
Return R;
}

In this way, once an error occurs in computing, the program will be terminated immediately, and the programmer will be reminded of the line in which the error occurred. Inserting some assert during code writing can effectively help programmers find errors. At the same time, you can stop the program when the error occurs to avoid expansion of the error.

Method 2: use dynamic debugging in IDE.
Many IDES (Integrated editing environments) have dynamic debugging functions, such as single-step running and variable monitoring. Some even provide registers, disassembly, and function call stacks. However, the first two methods are most frequently used.
Single-step running and variable monitoring are usually used in combination. Take the Chinese version of vc2003 as an example. After the program is compiled, each time you press the F10 key, you can execute a statement and then stop. At this time, the debugger can check which variables have changed and whether the changes are expected.
You can monitor variables by selecting "debug"> "window"> "Monitor" from the menu during a single step (other versions of VC are similar ), open the monitoring window and enter the expression to be monitored on the left of the window (for example, to monitor variable A, enter "A"; to monitor the value of A + 3, enter a + 3 ).
In addition to F10, there are several shortcut keys. If the row being run is a function, press F11 to run it inside the function. You can press SHIFT + F11 to run the function until it returns. Press F5 to run until the program ends. Of course, the program will also stop when there is a "breakpoint. When editing or debugging, you can press F9 to set or cancel a breakpoint for the row.
In this way, the processing of such statements as loops and if statements can be clearly understood. Even by monitoring the "function call stack", it also has some benefits in learning recursion.

Maybe I am not very clear about it, but I have to study debugging for beginners. Otherwise, the program compilation and connection may all pass, but a bunch of errors may run, and it will take a long time to continue learning.

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.