The application method of the C ++ assert Macro will be described in this article.ArticleFor details I believe that friends who are interested in this should be able to fully master the application skills based on the content we introduced. As an experienced programmerProgramming LanguageThere should be no strangers. Its Applications can help us easily implement various functional requirements. Here we will give a detailed introduction to some basic applications of the C ++ assert macro. The assert macro prototype is defined in <assert. h>. Its function is to terminate a condition if an error is returned.ProgramRun, Prototype definition: # Include <assert. h> void assert (INT expression ); The function of the C ++ assert () macro is to calculate the expression. If the value is false (that is, 0 ), Then it prints an error message to stderr first, Then, terminate the program by calling abort. See the following program list 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 writable mode. If it does not exist, create a file with the same name. Assert (FP); // so there will be no error Fclose (FP ); Fp = fopen ("noexitfile.txt", "R"); // open a file in read-only mode. If it does not exist, the file fails to be opened. Assert (FP); // an error occurs here Fclose (FP); // the program will never be executed here Return 0; } The disadvantage of using the C ++ assert () macro is that frequent calls will greatly affect program performance and increase additional overhead. After debugging, you can disable the assert call by inserting # define ndebug before the statement containing # include <assert. h>, ExampleCodeAs follows: # Include <stdio. h> # Define ndebug # Include <assert. h> This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/acdnjjjdjkdckjj/archive/2010/05/24/5621118.aspx |