Home > php教程 > PHP开发 > body text

Summary of assert() function usage

黄舟
Release: 2016-12-28 13:19:26
Original
1981 people have browsed it

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

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

The function of assert is to calculate 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;
}
Copy after login

[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 will greatly affect the performance of the program and increase additional overhead. After debugging, you can disable the assert call by inserting #define NDEBUG before the statement containing #include . The sample code is as follows:

#include <stdio.h>
#define NDEBUG
#include <assert.h>
Copy after login

Usage summary and precautions:

 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);
  ...
}
Copy after login

 2) Each assert only tests one condition, because when multiple conditions are tested at the same time, if the assertion fails, it cannot be intuitively judged. Which condition failed, such as:

Bad:

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

Good:

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

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

Error:

assert(i++ < 100);
Copy after login

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

Correct:

assert(i < 100);
 i++;
Copy after login

4) The assert and subsequent statements should be on one 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 usage of assert() function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!