Summary of assert of function usage in C++

  • 2020-05-26 09:49:28
  • OfStack

Summary of the usage of assert() function in C++

The prototype of the assert macro is defined in < assert.h > , its effect is to terminate the program execution if its condition returns an error, the prototype definition:

#include <assert.h>
void assert( int expression );

assert is used to evaluate the expression expression, and if it is false (that is, 0), it prints an error message to stderr and then terminates the program by calling abort.

See the following program listing badptr.c:

#include <stdio.h> 
#include <assert.h> 
#include <stdlib.h> 
int main( void ) 
    FILE *fp; 
    fp = fopen( "test.txt", "w" );// Open in a writable manner 1 Two files, created if none exist 1 File with the same name  
    assert( fp );              // So you can't go wrong here  
    fclose( fp ); 
    fp = fopen( "noexitfile.txt", "r" );// Open in read-only mode 1 File, if it does not exist, open the file failed  
    assert( fp );              // So this is a mistake  
    fclose( fp );              // The program will never execute 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 using assert() is that frequent calls can greatly affect the performance of the program, adding additional overhead. After debugging, you can do this by including #include < assert.h > To disable the assert call, insert #define NDEBUG before the statement,

The sample code is as follows:

#include <stdio.h>
#define NDEBUG
#include <assert.h>

Usage summary and precautions:

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

int resetBufferSize(int nNewSize) 
  // function : Change the buffer size , 
  // parameter :nNewSize  New buffer length  
  // The return value : Current buffer length   
  // instructions : Keep the original message content the same    nNewSize<=0 Means clear buffer  
  assert(nNewSize >= 0); 
  assert(nNewSize <= MAX_BUFFER_SIZE); 

2) each assert only tests one condition, because when multiple conditions are tested simultaneously, if the assertion fails, it is impossible to intuitively judge which condition fails, such as:


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


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

3) statements that change the environment cannot be used, because assert only takes effect at DEBUG. If you do so, you will encounter problems when the program is actually running, such as:


assert(i++ < 100);

This is because if there is an error, such as i=100 before execution, then this statement will not be executed, and i++ will not be executed.


 assert(i < 100);

4) assert and the following statements should leave 1 line blank to form a logical and visual sense of 1.

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

Here are a few 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 a false situation, the latter is inevitable and must be dealt with.

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

(3) when writing a function, examine it over and over again and ask yourself, "what assumptions am I going to make?" Once the assumptions have been determined, the assumptions are checked with assertions.

(4) 1 all textbooks encourage programmers to design error-proof programs, but keep in mind that this style of programming hides errors. When programming error-proofing, use assertions to alert if something "impossible" does happen.

ASSERT () is a macro commonly used in debugging a program. It evaluates the expression in parentheses while the program is running. If the expression is FALSE (0), the program reports an error and terminates execution. If the expression is not 0, proceed with the following statement. This macro is usually used to determine if there is clearly illegal data in the program, to stop the program if there is, so as not to cause serious consequences, and to look for errors.

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

A good place to use assert in a program:

(1) null pointer check. For example, null pointer checks are performed on the arguments of a function. You can use it like this: assert (pointer! = NULL); , the resulting error will look like this: Assertion 'pointer! = ((void *)0)' failed. This way, when a null pointer appears, your program will exit with a good error message.

(2) check the value of function parameters. For example, if a function can only be called if one of its arguments, foo, is positive, you can write this at the beginning of the function :assert (foo) > 0); This will help you detect the wrong use of the function and will give the source code reader the distinct impression that there are limits on the parameter values of the function.

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: