How to use the Visual C ++ debugger?

zhaozj2021-02-11  175

How to use the Visual C debugger?

Author: Jia Ying Yue compared with DOS programs, Windows programs easily reach tens of thousands of lines, hundreds of thousands of lines, commissioning quite complex. Fortunately, Visual C provides a powerful debugger that enables us to debug on the source code, assembly level. You can use asserts in debugging, and the Trace macro output combines single-step execution to comprehensively debug. 1. How to use compilation, the error message is shown in the figure, in the compilation, the connection phase Output window will output the currently compiled information to us, if you encounter an error, it will report the error in the first few lines, what is wrong? . At this time, double-click the left button on the error prompt line, you can locate the error in the program, and you can modify our code according to the error prompt. As this example is a syntax error - leaking a semicolon. Of course, sometimes the real error is not in this line (usually the correlation between a few errors). This requires us to look at the nearby lines. If the error is not understood, you can highlight the error prompt line, then press the F1 key, you can check the more detailed explanation of this error. (Of course, the premise is your English level is good) FAQ: ☆ Syntax error: Please check if the semicolon (row end value), if, ELSE match, switch statement usage is right. Note that the macro definition, the end of the included file definition does not require a semicolon, and the class definition requires a semicolon. ☆ Variables, functions are undefined, redefine: Please check the variable case, whether the corresponding header file (including your own and MFC, Windows). ☆ Connection error: This error typically occurs when using a dynamic linking library (DLL) in your program (whether you or Windows itself). At this point, it can be seen which function is wrong. For example, you call a Windows API, and the instructions in the MSDN write this function to include which header file (.h), enter which library (.lib), then add this library in your project settings. The method is to select the menu: project-> settings, go to the Link tab, enter the corresponding module in the Object / Library Modules input box, as shown. If we correct all the mistakes, compile, but the result is incorrect, then we must use debugging methods. There are two main types. 2. When you use the assertion to run the runtime debugging, debugging is to press CTRL F5 execution programs, test at runtime. (Press F5 when stepped by step) At this time our program and Debugger are "relative" independent. If your program is running well, or sometimes illegal operations, you feel that your design is not a problem, then it is best suited to use asserts to run time-up. The method is to use Assert Macros (MFC macro is not generated in the release of the program, only useful in the debug version). As a result, you judge a condition and assume it true. Some of the code we have written during the program always requires certain conditions, and the conditions are performed when they meet, and they do not do it. If your program is running well, or sometimes it is illegal, it must be due to the implementation conditions of some code, sometimes it is not established.

At this point, you can add the Assert macro judgment condition (usage: assert), which can be successfully executed in front of these code, when the condition meets, when the condition is not satisfied, the debugger pops up the window: At this time, Debugger tells We: "The assertion conditions of the 317th line of your program are not satisfied!" Haha, we know where we know. " Such an error typically occurs in a possible illegal object pointer, an array underbound, or the like, can cause illegal access to memory that is not a application. As simple as the assertion is simple, powerful, so we should develop the habit of joining the assertion when programming. There are several situations: ◎ Assign a memory through the pointer, the validity must be valid using the assertion judgment. Use Assert (POBJECT! = NULL); ◎ For function calls, the validity of the assertion judgment parameters must be used. These parameters include the number of pointers, limit ranges. ◎ For array access, the validity of the subscript must be used, especially when using the function to pass parameters. ◎ Other situations, please summarize yourself. Example: // Example for assertcage * pcage = new cage (21); // Cage is deive from cobject.

AskERT (PCAGE! = NULL)

Assert (pcage-> iskindof (runtime_class (CAGE)))

// Terminates Program Only IF PCAGE IS NOT A CAGE *. In addition, the MessageBox () function can be used to output a variable at runtime. 3, combined with Trace macro, set breakpoints to make single-step commissioning single-step debugging is the most effective way, although time, but it can go deep into the program, observe the execution of our program, execute the variable Value changes, so that we can understand the deficiencies of our programming ideas (after all, people 's thoughts are inconsistent with computer "thinking"). If you have never conducted single-step debugging, you can definitely say that you must have never compiled a big program (the applet can do it without bug, but the big program will still be wrong even after many people walking.). In the code editor, click Right click, you can choose to insert or remove a breakpoint in the pop-up menu, as shown: After setting several breakpoints needed, we can press F5 to start debugging. The window when debugging, the use is simple to introduce two questions: 1. TRACE usage: Trace is actually the same as the printf we used in the DOS program (the role and usage is almost). You can use information on the value of the TRACE output variable to facilitate your debugging. Lifting a simple example: // Example for Trace

INT i = 1;

Char sz [] = "one";

Trace ("Integer =% D, String =% S / N", I, SZ);

// Output: 'INTEGER = 1, String = One'

转载请注明原文地址:https://www.9cbs.com/read-5714.html

New Post(0)