Assertion (ASSERT) usage

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

  After a certain understanding of it, I have a certain understanding of its function and usage. The usage of assert() is like a kind of "contract programming". In my understanding, it means that the program is in my Under the assumption that it can work normally and well, it is actually equivalent to an if statement:

if (assume true)
{ the
program is running normally;
}
else
{
error && terminate the program! (Avoid bigger errors caused by the program running)
}
  But in this way, there will be an infinite number of if statements, and even there will be an if statement with parentheses from the beginning of the file to the end of the file, and in most cases, we have to do The hypothesis to verify is just a random event, or we just want to test whether some worst-case scenarios occur, so here is assert().

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

#include "assert.h"
void assert( int expression );
  The function of assert is to evaluate the expression expression, if its value is false (ie, 0), then it first prints an error message to stderr, and then calls abort to Terminate program execution.
The disadvantage of using assert is that frequent calls will greatly affect the performance of the program and add extra overhead.
  After debugging is over, assert calls can be disabled by inserting #define NDEBUG before the statement containing the #include, example code is as follows:

#include
#define NDEBUG
#include
  usage summary and precautions:

  1) Check the validity of the incoming parameters at the beginning of the function,
  such as :
  

int resetBufferSize(int nNewSize)
{
//Function: change the size of the buffer,
//parameter: nNewSize the new length of the buffer
//Return value: the current length of the buffer
//Description: keep the original information content unchanged nNewSize<=0 means clear buffer
assert(nNewSize >= 0);
assert(nNewSize <= MAX_BUFFER_SIZE);

...
}
  2) Each assert only checks one condition, because when multiple conditions are checked at the same time, if the assertion fails, it is impossible to intuitively judge which condition fails
  
  . Bad   : assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize);
  Good: assert(nOffset >= 0);
     assert(nOffset+nSize <= m_nInfomationSize);
 
  3) The statement that changes the environment cannot be used, because assert only takes effect in DEBUG. Problem
  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 the i++ command will not be executed.
  Correct: assert(i < 100)
      i++;
 
  4) The assert and the following statement should be a blank line to form a logical and visual sense of consistency
  
  5) In some places, assert cannot replace conditional filtering   
  
  Programs are generally divided into Debug version and Release version , the Debug version is used for internal debugging, and the Release version is released to users. Assertion assert is a Debug build-only macro that checks for something that "should not" happen. The following is a memory copy program that will abort during operation if the assert parameter is false (usually a prompt dialog will also appear indicating where the assert was triggered).

The following are several principles for using assertions:
  
  (1) Use assertions to capture illegal situations that should not occur. Don't confuse the difference between an illegal situation and an error situation, the latter must exist and must be dealt with.
  
  (2) Use assertions to confirm the parameters of the function.
  
  (3) When writing a function, it is necessary to conduct repeated examinations and ask yourself: "What assumptions do I intend to make?" Once the assumptions are determined, use assertions to check the assumptions.
  
  (4) General textbooks encourage programmers to design error-proof programming, but keep in mind that this style of programming hides errors. When programming for error proofing, use assertions to alert if the "impossible" thing does happen.

  ASSERT() is a macro that is often used when debugging a program. It evaluates the expression in parentheses when the program is running. If the expression is FALSE (0), the program will report an error and terminate execution. If the expression is not 0, the execution of the following statement continues. This macro usually judges whether there is obviously illegal data in the program, and if there is, terminates the program to avoid serious consequences, and it is also easy to find errors.

ASSERT is only valid in Debug builds and ignored if compiled to Release builds.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326064516&siteId=291194637