Summary of assert () function usage.

Source: Internet
Author: User
Tags prototype definition

Transferred from: http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html, thank you !!!

The assert macro prototype is defined in <assert. h>. Its function is to terminate a condition if an error is returned.ProgramExecution, prototype definition:

# Include<Assert. h>VoidAssert (IntExpression );

The role of assert is to calculate the expression. If its value is false (that is, 0), It prints an error message to stderr and then calls abort to terminate the program running. 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 the file 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 the file does not exist, it fails to be opened.    Assert (FP );  //  So here Error    Fclose (FP ); //  The program will never be executed here.      Return     0  ;} 

[Root @ localhost error_process] # GCC badptr. c
[Root @ localhost error_process] #./A. Out
A. Out: badptr. C: 14: Main: assertion 'fp' failed.

The disadvantage of having to stop using assert () 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>. For example:CodeAs follows:

# Include<Stdio. h># DefineNdebug# Include<Assert. h>


Usage summary and precautions:

1) Check the validity of input parameters at the beginning of the function, for example:

 Int  Resetbuffersize (  Int  Nnewsize ){  //  Function: Change the buffer size,  //  Parameter: New length of the nnewsize Buffer  // Returned value: the current length of the buffer.  //  Note: keep the original information unchanged. nnewsize <= 0 indicates clearing the buffer zone.    Assert (nnewsize  > =     0  ); Assert (nnewsize  <=  Max_buffer_size );...} 

2) Each assert tests only one condition, because when multiple conditions are verified simultaneously, if the assert fails, you cannot intuitively determine which condition fails, for example:

Bad:

Assert (noffset> =0 &&Noffset+Nsize<=M_ninfomationsize );

Good:

Assert (noffset> = 0); Assert (noffset+Nsize<=M_ninfomationsize );

3) You cannot use statements that change the environment, because assert takes effect only on Debug. If you do so, problems will occur when the program is actually running, such:

Error:

Assert (I++ < 100);

This is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed.

Correct:

  assert (I       1.5   ); I   ++  ;  

4) The Assert and subsequent statements should have a blank line to form a logical and visual sense of consistency.

5) In some cases, assert cannot be used as a condition filter. assert is used to avoid obvious errors, rather than handling exceptions. Errors are different from exceptions. Errors should not occur and exceptions are inevitable. C language exception can be handled through condition judgment. Other languages have their own exception handling mechanisms.

A very simple rule of using assert is to be used at the beginning of a method or function, if you use it in the middle of the method, you need to carefully consider whether it is appropriate. The method has not started a functional process at the beginning, and the problems encountered during the execution of a function are almost all exceptions.

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.