I always thought assert was just an error reporting function, but in fact, it is a macro, and its purpose is not "error reporting".

After gaining some understanding of it, I found that the usage of assert() resembles a kind of "contract programming". In my understanding, it means that the program can operate normally and well under my assumptions. In fact, it is equivalent to an if statement:

if(假设成立)
{
     程序正常运行;
}
else
{
      报错&&终止程序!(避免由程序运行引起更大的错误)  
}

But writing it this way would result in countless if statements, and it could even happen that an if statement's parentheses span from the beginning to the end of a file. Moreover, in most cases, the assumptions we need to verify are only incidental events, or we simply want to test whether some worst-case scenarios occur. That's why assert() exists.

The prototype of the assert macro is defined in assert.h. Its function is to terminate program execution if its condition returns an error.

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

The role of assert is to first evaluate the expression expression; if its value is false (i.e., 0), it first prints an error message to stderr, then terminates program execution by calling abort.

The disadvantage of using assert is that frequent calls can greatly affect program performance and add extra overhead.

After debugging, you can disable assert calls by inserting #define NDEBUG before the #include statement. Example code is as follows:

#include 
#define NDEBUG 
#include

Usage Summary and Precautions

1) Check the validity of incoming parameters at the beginning of the function.

For example:

int resetBufferSize(int nNewSize) 
{ 
//功能:改变缓冲区大小, 
//参数:nNewSize 缓冲区新长度 
//返回值:缓冲区当前长度 
//说明:保持原信息内容不变 nNewSize<=0表示清除缓冲区 
assert(nNewSize >= 0); 
assert(nNewSize <= MAX_BUFFER_SIZE); 
 
... 
}

2) Each assert should check only one condition, because when checking multiple conditions at once, if the assertion fails, it is impossible to intuitively determine which condition failed.

     

Not good:

assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize); 
    

Good:

assert(nOffset >= 0); 
assert(nOffset+nSize <= m_nInfomationSize); 

3) Do not use statements that change the environment, because assert only takes effect in DEBUG. If you do this, it will cause problems when the program actually runs.

Wrong: assert(i++ < 100)

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

Correct:

assert(i < 100)
i++; 

4) There should be a blank line between assert and the following statement to form a logical and visual sense of consistency.

  

5) In some places, assert cannot replace conditional filtering.

  

Programs are generally divided into Debug versions and Release versions. Debug versions are used for internal debugging, and Release versions are distributed to users. The assert assertion is a macro that only works in Debug versions; it is used to check for situations that "should not" happen. The following is a memory copy program. During execution, if the parameter of assert is false, the program will terminate (usually a prompt dialog appears, indicating where the assert was triggered).

The following are several principles for using assertions:

  • (1) Use assertions to catch illegal situations that should not happen. Do not confuse the difference between illegal situations and error situations; the latter necessarily exist and must be handled.
  • (2) Use assertions to confirm the parameters of functions.
  • (3) When writing functions, repeatedly examine and ask yourself: "What assumptions do I intend to make?" Once an assumption is determined, use assertions to check the assumption.
  • (4) General textbooks encourage programmers to carry out defensive programming, but remember that this programming style can hide errors. When performing defensive programming, if something that "cannot happen" does happen, use assertions to raise an alarm.

ASSERT() is a macro often used when debugging programs. At runtime, it evaluates the expression inside the parentheses. If the expression is FALSE (0), the program will report an error and terminate execution. If the expression is not 0, it continues executing subsequent statements. This macro is usually used to determine whether obviously illegal data has appeared in the program; if it has, it terminates the program to avoid serious consequences, and it also facilitates error finding.

ASSERT is only valid in Debug versions; it is ignored if compiled as a Release version.

Original URL: https://www.cnblogs.com/thisway/p/5558914.html