Assert assert in C language

Source: Internet
Author: User
Tags terminates

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:

if (assuming) {The     program runs normally;}else{      error && Abort program! (Avoid a larger error caused by the program's operation)  }

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.

#include "assert.h" void assert (int expression);

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:

#include #define NDEBUG #include

  Usage Summary and Precautions:

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

int resetbuffersize (int nnewsize) {//function: Change buffer size,//parameter: Nnewsize buffer new Length//return value: Buffer current Length//Description: Keep the original information content unchanged nnewsize<= 0 means clear buffer assert (nnewsize >= 0); ASSERT (Nnewsize <= max_buffer_size); ...  

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) You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into problems when it is actually running
Error: Assert (i++ < 100)
This is because if there is an error, such as i=100 before execution, then this statement will not be executed, then i++ This command will not be executed.
Correct: Assert (I < 100)
i++;
 
4) Assert and subsequent statements should be empty lines to create a logical and visual sense of consistency
  
5) In some places, assert cannot replace conditional filtering  
  
The program is generally divided into debug version and release version, debug version for internal debugging, release version released to the user. Assert assert is a macro that works only in the debug version, and it is used to check what should not happen. The following is a memory copy program that, during the run, if the assert parameter is False, then the program aborts (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.

Assert is only valid in debug versions and is ignored if compiled to release version.

Assert assert in C language

Related Article

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.