VC debugging skills

Source: Internet
Author: User
Tags microsoft website
Basic compiler skills: F10: single-step debugging, step-by-step execution of the program, generally used to view the program execution process, if the program is broken from it, you can use single-step debugging. F9: When a breakpoint is set, the program stops when it is executed to the place where the breakpoint is set. F5: run the debugging program, Debug | Go. F11: Enter the block for debugging. Ctrl + F5: in use

Basic compiler skills: F10: single-step debugging, step-by-step execution of the program, generally used to view the program execution process, if the program is broken from it, you can use single-step debugging. F9: When a breakpoint is set, the program stops when it is executed to the place where the breakpoint is set. F5: run the debugging program, Debug | Go. F11: Enter the block for debugging. Ctrl + F5: in use

Basic compiler skills:

F10: One-Step debugging, step-by-step execution of the program, which is generally used to view the program execution process. If the program is disconnected from it, you can use one-step debugging.

F9: When a breakpoint is set, the program stops when it is executed to the place where the breakpoint is set.

F5: run the debugging program, Debug | Go.

F11: Enter the block for debugging.

Ctrl + F5: Execute the debugging program, Debug | Execute.

Ctrl + F7: compile a single file without compiling all the files. This avoids compilation of unnecessary files and increases the Compilation Time.

Clean: Clear the project

Rebuild all: Delete the generated intermediate file and recompile the entire project.

Watch window: Add a variable to the watch window, and you can view the value of the variable, but you cannot view the return value of the function. Generally, you can view the return value of the function. You should save it in the variable and view it.

Call Stack: call the function stack. In the Call Stack, you can view the call relationships between functions at all levels, and locate bugs and errors.

Shift + F5: cancels the debugging process.

Ctrl + B: Set the data breakpoint and location breakpoint.

Location breakpoint:

This is a method we often use. We set a breakpoint on the code line we want to debug, and then press F5 or F10 for debugging. This method is feasible in non-circular code blocks, however, if it is in a loop, it will be a little troublesome. In this case, you need to press F5 multiple times. If the number of cycles exceeds 1000, think about it, if you press F10 for single-step debugging, that is, pressing F5 also consumes a lot of energy. here we will introduce a method:

That is, the conditional attention method of the breakpoint.

Set the code lines to debug.

Press ctrl + B to open the breakpoint Setting Dialog Box. In this case, we can see a location. In the breakpoints list below, we can select the line of code for debugging. In this case, on the above page, we can see the code line we just selected in the break at box, and then select it in the condition button below, enter an expression in "Enter theexpression to be evaluated", which indicates that the breakpoint can be started only when the expression is true. of course, if you enter a variable name, the breakpoint can be started only when the variable is modified. there are also two items: "Enter the number of elements to watch in an arrayor structure" and "Enter the number of times to skip before stopping ". you can set some options here, which are very useful for loop blocks. the first item indicates the number of items to be observed in an array or structure, and the second item indicates the number of times to be skipped before termination. It is obviously useful when there are many cycles, here we can set it to the number of cycles. In this way, after all the information is set, press F5 and the program will be interrupted, then, we can see a message "remainingxx" in "breakpoints", which indicates that it is terminated after 100-xx + 1 in a loop, of course, we can also use the "Enter theexpression to be evaluated" method to directly obtain information from the value, rather than the number of times.

For (int I = 100; I> 0; I --){

Printf ("% d", 100/(I-5 ));

}

This is an example.

Data breakpoint:

That is, the method "Enter the expression to be evaluated", that is, when a condition value is met, the breakpoint will start.

In fact, the position breakpoint and the data breakpoint are complementary. Relatively speaking, the data breakpoint is more suitable for determining the situation where the data is modified, and the position breakpoint is generally used in a loop, to describe the execution of the program. an error occurred at that location. Locate the location and locate the error.

Another method is to set a breakpoint for a function in callstack. In this way, function calls can be returned from the depth level.

In the debugging status, right-click A code line A and choose set next statement from the context menu. In this way, the next code line is, instead of other code lines, you can understand it literally.

In addition, the watch window can also be used to monitor variable value changes, which is very useful. Especially for some arrays and structures, you can view the values of all elements in the array/structure, however, when we have used up, we have to delete the selected variables, because they will appear next time.

Print log:

You can print related information by using the output function to output log information to the screen. These methods include:

Prinf, cout, MessageBox, and OutputDebugString. Of course, you can also record the information in the text for future observation, and write the information into a file using fopen and the like.

Find the crash address:

You can use dbghelp. For more information, visit the official Microsoft website.

You can also use the map file. Here is a good introduction.

Http://www.codeproject.com/KB/debug/mapfile.aspx

Use assertions:

Assertions can help you check data validity more, but note that you should not use functions in assertions, because assert is invalid in non-debug mode, such as release mode, to skip the execution.

Usage exception:

Try/catch/throw, and _ try/_ catch/_ throw.

Other debugging information and related compiler settings:

Http://ei.cs.vt.edu /~ Cs1205/c_debug/intro.html

Http://www.hermetic.ch/cfunlib/debug.htm

Http://www.gamedev.net/reference/articles/article1344.asp

Recommended programming habits:

Assert checks the data precursor and successor, uses defense programming, and checks data at the function entry and function exit.

When applying for memory or allocating space, check whether the data is allocated successfully. If the data is not allocated successfully, perform related processing.

When declaring a variable, it is best to initialize it to prevent unknown values from being obtained without initialization.

Uninitialized memory areas are generally filled with 0xcccccccc, and the areas in which memory has been released are generally 0xcdcdcdcd.

If you try to access a pointer with a data value of 0xcdcdcd, it means that you have released it somewhere. If it is 0 xcccccccccc, this means that the pointer has not been allocated space.

(1) If the pointer disappears, it does not mean that the memory it refers to will be automatically released. (From Dr. Lin's high-quality C ++ programming)

(2) The memory is released, which does not mean that the pointer will die or become a NULL pointer.

Here we can understand:

1) For the memory area on the stack, it will be invalid after the pointer leaves the scope, but it does not mean that the memory occupied by the pointer is automatically released. On the contrary, memory leakage occurs.

2) After the memory is released, the pointer is still not empty, so you must leave it empty to avoid the occurrence of a wild pointer.

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.