How many macros for debugging procedures in VC

xiaoxiao2021-03-05  31

The use of several macros for debugging programs in the VC is yellow, and the Trace Macro is selected when the Debug target is selected, and the AFXTRACEENABLED variable is set to TRUE. But in the RELEASE version of the program, they are completely prohibited. Here is a typical trace statement: int ncount = 9; CString strdesc ("total"); trace ("count =% d, description =% s / n", ncount, strdesc); you can see that the TRACE statement work The way is a bit like the PrintF statement in the C language, and the number of trace macro parameters is variable, so it is very easy to use. If you look at the source code of the MFC, you can't find the Trace Macro, but you can only see Trace0, Trace1, Trace2, and Trace3 macros, their parameters are 0, 1, 2, and 3, respectively. Second, Assert Macro If you design a function, the function needs a pointer to the document object to do parameters, but you are incorrectly invoking this function with a view pointer. This fake address will result in damage to the data. This type of problem can now be completely avoided, as long as an Assert test is implemented at the beginning of this function, used to detect if the pointer really points to a document object. Generally speaking, the programmer uses Assertion on the start of each function. Assert Macro will judge the expression. If an expression is true, the execution will continue, otherwise, the program will display a message and pause, you can choose to ignore this error and continue, terminate this program or jump to the Debug. In the following example, how to use an ASSERT macro to verify a statement. Void foo (char p, int size) {assert (p! = 0); // Check the pointer to the buffer is a valid assert ((size> = 100); // Confirm that the buffer has at least 100 bytes / / Do the foo calculation} These statements do not generate any code unless the -debug processor flag is set. Visual C only sets these flags only in the debug version, and these flags are not defined in the Release version. When -debug is defined, two Assertions will produce the following code: // assert (p! = 0); do {if (! (p! = 0) && afxassertfailedLine (-file -, - line -)) AFXDebugBreak ();} while (0); / / Assert ((size> = 100); do {if (! (Size> = 100) && afxassertfailedLine (-file -, - line -)) AFXDebugBreak ();} while (0); D0-while loop will be the entire assertion Package in a separate block, make the compiler compile very comfortable. IF statement will find the value of the expression and call the AFXASSERTFAILEDLINE () function when the result is zero. This function will pop up a dialog box, which provides three Option "Cancel, Retry or Ignore" When you select "Retry", it will return true. Retry will result in the call to the AFXDebugBreak () function, to activate the debugger. AfxassertFailedLine () is an unaprimated Functions, its function is to display a message box. The source code of the function resides in Afxasert.cpp.

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

New Post(0)