Overview Debugging is the most basic skill of programmers, and its importance even exceeds learning a language. The programmer who will not debug means that he will not prepare any good software even if he will speak in a language. Here I briefly lists the more common skills in debug according to their own experience, I hope to use it for everyone. This article contemplates, when selecting a menu, by / representing a hierarchical menu, such as File / Open, a submenu Open of the top-level menu file. Set to debug a program, you must first make the program contain debug information. In general, a debug configuration included in the project created from AppWizard automatically contains debugging information, but is the Debug version is not a program contains determinants of debugging information, and program designers can add debugging information in any Configuration, including Release. version. In order to increase the debugging information, you can follow the steps below:
Open the Project Settings dialog (can be opened by shortcut OLT F7, or open by IDE menu Project / Settings) Select C / C page, select General, select General, appears, a debug, optional debugging Information methods include: Command line Project Settings Description None None without debugging information / zd Line Numbers Only Target file or executable only contains global and export symbols and code row information, not including symbol debugging information / z7 C 7.0-Compatible target file Alternatively, the executable contains the line number and all symbol debug information, including variable name, functions, and prototypes / Zi Program Database Create a library (PDB), including type information and symbol debugging information. / Zi Program Database for Edit and Continue In addition to the function of the front / zi, this option allows the code to be modified and continued during the debugging process. This option simultaneously invalidates the optimization function set by #pragma.
Select the link page, select the check box "Generate Debug Info, this option will make the connector to write the debug information into the executable and DLL, if the PROGRAM DATABASE above the C / C page, Link Incrementally can choose. Select this option, which will make the program can be compiled (ie, incremental compilation) on the basis of the last compilation, without having to start compilation from the head every time. The breakpoint breakpoint is a code location set by the debugger. When the program is running to the breakpoint, the program is interrupted, returns to the debugger. Breakpoint is the most common skill. When debugging, only the breakpoint is set and the program will be commissioned online.
Set breakpoints: You can set a breakpoint by the following method. First move the cursor to the code line that needs to set the breakpoint, then press the F9 shortcut to pop up the BreakPoints dialog box, the method is to press the shortcut key Ctrl B or Alt F9, or open by menu Edit / Breakpoints. After opening, click the arrow on the right side of the BREAK AT Edit box, select the appropriate location information. Under normal circumstances, directly selecting Line XXX is enough. If you want to set a breakpoint that is not the current location, you can select Advanced, then fill in functions, line numbers, and executable information. Remove the breakpoint: Move the cursor to the line where the break point is located, and press F9 again to cancel the breakpoint. Once the BreakPoints dialog is opened as described above, you can also remove the breakpoint in accordance with the interface prompt.
Condition breakpoint: You can set a condition for breakpoints, such a breakpoint called condition breakpoint. For newly added breakpoints, you can click the Conditions button to set an expression for the breakpoint. When this expression changes, the program is interrupted. Under the bottom setting includes "Observing an array or structure", it seems that the size of the memory area points to which a pointer points can be set, but I set a comparison value but the memory area outside the change range seems to also result in breakpoints. The last setting allows the program to execute how many times will then reach the breakpoint. Data breakpoint: Data breakpoint can only be set in the BreakPoints dialog. Select the "DATA" page to display the dialog box for setting up data breakpoints. Enter an expression in the edit box, when the value of this expression changes, the data breakpoint is arrived. In general, this expression should be composed of operators and global variables, for example, enter the name of G_bflag's global variable in the edit box, then there is g_bflag =! G_bflag in the program, the program will stop at this statement.
Message breakpoint: VC also supports interception of Windows messages. He has two ways to intercept: Window message processing function and a specific message interrupt. When you select your message breakpoint in the BreakPoints dialog box, you can set the message breakpoint. If you write the name of the message processing function in the above dialog, each message is processed by this function, the breakpoint arrives (I think if it is intercepted in this function, the effect should be the same). If a message is selected at the drop-down list box below, each time this message arrives, the program is interrupted.
Value WatchVC supports the value of the viewing variables, expressions, and memory. All of these observations must be carried out in the case of breakpoint interruption. The value of watching the variable is the simplest, when the breakpoint arrives, move the cursor to this variable, and stay in a while, you can see the value of the variable. VC provides a mechanism that is Watch to view variables and expressions. In the breakpoint state, right-click on the variable, select Quick Watch, pop up a dialog, display the value of this variable. Click the WATCH button on the debug toolbar, there is a Watch view (Watch1, Watch2, Watch3, Watch4), enter variables or expressions in this view, you can observe the value of variables or expressions. Note: This expression does not have side effects, such as operators absolutely for use in this expression because this operator will modify the value of the variable, resulting in the logic of the software to be destroyed.
Memory only shows the value of the first element due to the array pointed to by the pointer. In order to display the subsequent content of the array, or display the contents of the memory, the Memory function can be used. On the Debug toolbar, point the Memory button, pop up a dialog box, enter the address in it, you can display the contents of the memory pointing to by.
The VaribleSDebug tool bar pops up a box that displays all the values of the variables visible in the context. In particular, the current instructions involved, in red display.
The REIGSTERS button on the Register Debug tool bar pops up a box to display the value of all current registers.
Process Control VC allows the program to be interrupted to continue to run, single-step operation and running to the specified cursor, respectively, and corresponding shortcuts F5, F10 / F11, and CTRL F10. Each shortcut key function is as follows: Shortcut Key Description F5 Continue to run the F10 single step,
The Call Stack call stack reflects that the current breakpoint function is called by those functions. Click the call stack on the Debug toolbar to display the Call Stack dialog. A call series is displayed in the CallStack dialog, the top is the current function, and the downlink is the superior function of the call function. Click these function names to skip to the corresponding function. Other debug means systems provide a series of special functions or macros to process the DEBUG version-related information, as follows:
Macro Name / Function Name Description Trace use method and printf is exactly the same, and he outputs debugging information in the Output box. Assert it receives an expression. If this expression is TRUE, no action, otherwise the current program is executed. For interruptions caused by this macro in the system, you should think that your function calls fail to meet the prerequisites of this function to call the system. For example, call setWindowText, etc. for a window that has not yet been created. Verify and assert features are similar, and in fact, in the Release version, Assert does not calculate the value of the input expression, and verify calculates the value of the expression.
Focusing on a good programmer should not give all judgments to the compiler and debugger, should be protected and error positioning in the program, including:
For all functions with return values, you should check the return value unless you are sure that this function call is absolutely worse, or if it doesn't care if it is wrong. Some functions returns an error and require other functions to get the error specific information. For example, Accept returns to invalid_socket indicates that the ACCEPT failed, in order to identify the specific failure, you should immediately use WsageTlasteError to get the error code, and targeted the problem. Some functions throw an error by an exception mechanism, should use the try-catch statement to check the error programmer for the process of handling, should handle it in the underlying, for the unable to process, should report to the user to make them decide how to deal with it. If the program is abnormal, it does not judge the error message returned to the return value and other mechanisms, which can only be increased to find an error. In addition: The program should not write CPP / H files at the beginning, and should first create a suitable project. Because only this, VC can choose the appropriate compilation, connection option. For CPP files joined in the project, you should check if the first line explicitly contains the stdafx.h header file, which is the pre-translated header file set by Microsoft Visual Studio in order to speed up compilation speed. All code in front of this #include "stdafx.h" will be ignored, so other header files should be included later. For the .c file, since it cannot contain stdafx.h, it can be set to "not" via Project Settings, the method is: popping up the Project Settings dialog box Select C / C category to select the precompilation header to choose not to use Compiling the head. // from vckbase