Use of trace macros and assert () functions in C + +

Source: Internet
Author: User
Tags assert

Use of trace macros in C + +

Trace macro for VC under the program debugging is very useful things, with a similar function of printf, the macro is only in the debug version of the program appears, when the release of the macro disappears completely, thus helping you debug also in the release of the time to reduce the amount of code.
The use is very simple and the format is as follows:
TRACE ("ddddddddddd");
TRACE ("wewe%d", 333);
There are also trace0,trace1,trace2 ... correspond to 0,1,2 respectively. A parameter
The trace information is output to the Output window of the VC IDE environment (which is the window you are prompted to compile the project error), but only if you are running the debug version of your program in VC.
Trace information can also be captured using DebugView. In this case, you can not run your program in the VC IDE environment, and the build good debug version of the program run alone, this time in the DebugView window to see the output of the Debugvie format.
The use of trace in VC has the following four kinds:
1:
TRACE, which is a C-like printf ("output string") without a dynamic parameter output string;

2:
The string in TRACE can be output with a parameter, like C's printf ("...%d", variable);

3:
TRACE can take two parameter outputs, such as C's printf ("...%d...%f", variable 1, variable 2);

4:
TRACE can take three parameter outputs, such as C's printf ("...%d,%d,%d", variable 1, variable 2, variable 3);
TRACE macros are a bit like the printf function we used in C, which allows the program to output some debugging information during the run, so that we can understand some of the state of the program. But there's a difference:

The TRACE macro is output only in the debug state, and the previously used printf function has output in all cases. Like the printf function, the trace function can accept multiple parameters such as:
int x = 1;
int y = 16;
float z = 32.0;
Trace ("This is a TRACE statement\n");
TRACE ("The value of X is%d\n", x);
TRACE ("x =%d and y =%d\n", x, y);
TRACE ("x =%d and y =%x and z =%f\n", x, Y, z);
Note that the trace macro only works on the debug version of the project, and the trace macro is ignored in the release version of the project.

The use of TRACE macros ...

TRACE macros are a bit like the printf function we used in C, which allows the program to output some debugging information during the run, so that we can understand some of the state of the program. But there's a difference:
The TRACE macro is output only in the debug state, and the previously used printf function has output in all cases. Like the printf function, the trace function can accept multiple parameters such as:

int x = 1;
int y = 16;
float z = 32.0;
Trace ("This is a TRACE statement\n");
TRACE ("The value of X is%d\n", x);
TRACE ("x =%d and y =%d\n", x, y);
TRACE ("x =%d and y =%x and z =%f\n", x, Y, z);

Note that the trace macro only works on the debug version of the project, and the trace macro is ignored in the release version of the project.

----------------------------------------------------------------------------------assert () Application of Functions-----------------------------------------------------------------------------------------------------------

The way the C + + assert () function is applied will be explained in this article, and it is believed that interested friends should be able to fully grasp the application skills in this area according to the content we introduce.
As an experienced programmer, the C + + programming language should not be unfamiliar, its application can help us to easily implement a variety of functional requirements. Here we will make a detailed introduction to some basic applications of the C + + assert () function.

The prototype of an Assert macro is defined in < assert.h>, and its function is to terminate the execution of the program if its condition returns an error, and the prototype defines:

#include < assert.h> void assert (int expression); The function of the C + + assert () function is to evaluate the expression expressions, if the value is False (that is, 0), then it prints an error message to stderr, and then terminates the program by calling abort. Take a look at the following program listing BADPTR.C:

#include < stdio.h> #include < assert.h> #include < stdlib.h> int main (void) {FILE *fp; fp = fopen ("Test.txt", "w");//Open a file in a writable manner and create a file assert (FP) with the same name if it does not exist;   So there's no error here. fclose (FP); fp = fopen ("Noexitfile.txt", "R");//Opens a file in a read-only manner, and if it does not exist, the file fails assert (FP); So here's the error fclose (FP);   The program will never execute here to return 0; } [[email protected] error_process]# gcc badptr.c [[email protected] error_process]#./a.out A.out:badptr.c:14:mai N:assertion ' FP ' failed. has been abandoned

The disadvantage of using the C + + assert () function is that frequent calls can greatly affect the performance of the program and add additional overhead. After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include < Assert.h>, as shown in the following example code:

#include < stdio.h> #define NDEBUG #include < assert.h>

Use of trace macros and assert () functions in C + +

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.