Home  >  Article  >  Web Front-end  >  Summary of assert() function usage (recommended)

Summary of assert() function usage (recommended)

高洛峰
高洛峰Original
2017-02-03 14:29:181695browse

The prototype of the

assert macro is defined in b0f0cbacb9a934b30cb1dd71186a65c7. Its function is to terminate program execution if its condition returns an error. The prototype definition:

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

assert The function is to evaluate the expression expression. If its value is false (that is, 0), then it first prints an error message to stderr, and then terminates the program by calling abort. Please 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" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
    assert( fp );              //所以这里不会出错
    fclose( fp );
    fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
    assert( fp );              //所以这里出错
    fclose( fp );              //程序永远都执行不到这里来
    return 0;
}
[root@localhost error_process]# gcc badptr.c
[root@localhost error_process]# ./a.out
a.out: badptr.c:14: main: Assertion `fp&#39; failed.

The disadvantage of abandoning the use of assert() is that frequent calls will greatly affect the performance of the program and add additional overhead. After debugging, you can disable the assert call by inserting #define NDEBUG before the statement containing #include b0f0cbacb9a934b30cb1dd71186a65c7. The sample code is as follows:

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

Usage Summary and Notes Matters:

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

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

2) Each assert only tests one condition, because multiple tests are performed at the same time When there are two conditions, if the assertion fails, it is impossible to intuitively determine which condition failed, such as:

Bad:

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

Good:

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

3) You cannot use statements that change the environment, because assert only takes effect in DEBUG. If you do this, you will encounter problems when the program is actually running, such as:

Error:

assert(i++ < 100);

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

Correct:

assert(i < 100);
i++;

4) The assert and following statements should be an empty line to create a sense of logical and visual consistency.

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

The above is a summary of the assert() function usage introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time.

For more assert() function usage summary (recommended) related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn