ASSERT () function method summary (recommended)

ASSERT macro prototype definition in , its effect is if its condition returns an error, the termination program is executed, prototype definition:

ASSERT’s function is the current calculation expression expression, if its value is false (ie 0) Then, then print an error message to stderr and then terminate the program by calling Abort. Please see the following program list Badptr.c:

#include #include
 int main (void) {file * fp; fp = fopen ("Test.txt", "W"); // Open a file in a writable manner, if there is no existence, create a single file assert (fp); //, so you will not be wrong Fclose (FP) fp = fopen ("noExitfile.txt", "r"); // open a file in a read-only manner, if there is no existence, open the file failed assert (fp); //, here, this error Fclose (FP); / / The program will never perform Return 0;}      [root @ localhost error_process] # gcc badptr.c [root @LocalHost Error_Process] # ./a.outA.out: Badptr.c: 14: main: assertion `fp 'failed. 
The disadvantage of using Assert () is that frequent calls will be great Affect the performance of the program, add additional overhead. After the debugging is over, you can disable the assert call by inserting #define ndebug before the statement containing #include
, the sample code is as follows:
#define NDEBUG # include

summary and usage Notes:
   1) test passed at the beginning of the function The legitimacy of the parameters is as follows:   
INT RESETBUFFERSIZE (INT NNEWSIZE) {// Function: Change Buffer Size, // Parameter: NNewsize Buffer New Length // Return Value : Buffer current length // Description: Keep the original information constant nnewsize = 0); assert (nnewsize

2) Each Assert only checks one condition When testing multiple conditions at the same time, if the assertion fails, it cannot be visually determined which condition failed, such as:

   Assert Noffset> = 0 && Noffset + NSIZE <= MAX_BUFFER_SIZE);

Assert (Noffset> = 0); Assert Noffset + nsize

 3) You cannot use a statement that changes the environment, because Assert is only in the debug, if you do it, you will use the program to encounter problems during truly, such as:  <=m_nInfomationSize); error : 

Assert (i ++

 This is because if it is wrong, for example, before execution I = 100, then this The statement will not be executed, then this command is not executed.  <= m_nInfomationSize); Correct: 

Assert (i

4) Assert and the following statement should be empty to form logic and visual consolidation.
  5) Some places, Assert cannot replace conditions filtration. < 100); 
The above is a summary of the assert () function to introduce you to everyone. I hope to help everyone. If you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment