Usage of assertion (assert)

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html

I always thought that assert is just an error function, in fact, it is actually a macro, and the role is not "error." After a certain understanding of its role and use of a certain understanding of the use of assert () is like a "contractual programming", in my understanding, the meaning is that the program under my assumptions, can operate normally good, in fact, the equivalent of an IF statement:

1 if (if established) 2 {3      program runs normally;4}5Else6{  7       error && termination procedure! (Avoid larger errors  due to program run)8 }
View Code

But in this case, there will be countless if statements, even if the parentheses of an if statement go from the file header to the end of the file, and in most cases we have to validate the hypothesis that it is just an accidental event, or if we just want to test that some of the worst cases happen, so here is the assert (    ). The prototype of an Assert macro is defined in assert.h, and its function is to terminate the program execution if its condition returns an error.

1 1 " assert.h " 2 2 void int expression);
View Code

The function of an assert is to evaluate 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.
The disadvantage of using assert 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, as shown in the following example code:

12#define3 #include

Usage Summary and Precautions:

1) Verify the legitimacy of incoming parameters at the beginning of the function
Such as:

 1 intResetbuffersize (intnnewsize)2 {  3 //function: Change buffer size, 4 //parameter: Nnewsize buffer new Length 5 //return value: Buffer Current length 6 //Note: Keep the original information content unchanged nnewsize<=0 means clear buffer 7ASSERT (Nnewsize >=0); 8ASSERT (Nnewsize <=max_buffer_size); 9  Ten ...  One}

2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed  
 
Bad:  assert (noffset>=0  && noffset+nsize<=m_ninfomationsize);   
Good:  assert (noffset >= 0) ;  
 assert (noffset+nsize <= m_ninfomationsize);  
 
3) cannot use statements that alter the environment. Because the assert only takes effect in debug, if you do, you will use the program to run into a problem  
Error:  assert (i++ < 100)  
This is because if an error occurs, such as i=100 before execution, then this statement will not be executed, then i++ This command is not executed.  
Correct:  assert (i < 100)  
   i++; 
 
4) The Assert and the following statements should be empty lines to form a logical and visual sense of consistency  
 
5) in some places, assert is not a substitute for conditional filtering    
 
programs are generally divided into debug& nbsp; version and release  version,debug  versions are used for internal debugging,release  release to users. Assertion assert  is a macro that works only in the debug  version, and it is used to check for situations where "should not" occur. The following is a memory copy program that, during the run, if the parameters of the assert  are false, then the program will be aborted (there will generally be a prompt dialog stating where the Assert was raised).  

here are a few principles for using assertions :

(1) Use assertions to catch illegal situations that should not occur. Do not confuse the difference between the illegal situation and the wrong situation, the latter being inevitable and must be dealt with.

(2) Use assertions to confirm the parameters of the function.

(3) When writing a function, repeat the examination, and ask yourself: "What assumptions do I intend to make?" Once the assumptions have been determined, the assumptions are checked using assertions.

(4) General textbooks encourage programmers to design for error-proof, but keep in mind that this style of programming conceals errors. When error-proof programming occurs, an assertion is used to alert you if the "impossible" thing does happen.

ASSERT () is a macro that is used frequently when a debugger is run, and it calculates the expression in parentheses when it runs, and if the expression is False (0), the program reports an error and terminates execution. If the expression is not 0, the following statement continues. This macro usually turns out that there is clearly illegal data in the program, and if a termination procedure is used to avoid serious consequences, it is also easy to find errors.

Usage of assertion (assert)

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.