How to use the assert function under linux system?

  

As long as people who understand the program know the assert, write it in VC under Windows. After using assert, just set it to debug or release in the IDE, the compiler will automatically ignore the assert. However, under Linux, you must add -DNDEBUG to the compile command, and the system will not compile the assert when compiling. Otherwise, the program may be interrupted by assert when it runs. How to use the assert function under the Linux system? Let me talk about the method of using the assert function under the Linux system.

following steps:

Prototype assert macro defined in "assert.h", whose role is to return an error condition if it is, the program execution is terminated, prototype definition: < Br>

#include "assert.h"

void assert( int expression );

The effect of assert is to evaluate expression now, if its value is false (ie 0 ), then it prints an error message to stderr,

and then terminates the program by calling abort.

Please see the following list of programs badptr.c:

#include "stdio.h"

#include "assert.h"

#include Stdlib.h

int main( void )

{

FILE *fp;

fp = fopen( “test.txt”, “w”);//Open a file in a writable way, if it does not exist, create a file with the same name

assert( fp ); //So there will be no error here

fclose ( fp );

fp = fopen( “noexitfile.txt”, “r” );//Open a file as read-only, failing to open the file if it doesn't exist

assert( fp ); //So here is an error

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.

Abandoned

The disadvantage of using assert is that it is frequent Calls can greatly affect the performance of the program, adding extra overhead.

After debugging, you can disable the assert call by inserting #define NDEBUG before the statement containing #include "asser.h". The sample code is as follows:

#include "stdio.h

#define NDEBUG

#include "assert.h"

Usage Summary and Notes:

1) Verify incoming at function start The legality of the parameters

For example:

int resetBufferSize(int nNewSize)

{

//Function: change the buffer size,

//parameters: nNewSize buffer new length

//return value: the current length of the buffer

//Description: keep the original information unchanged 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 at the same time, if the assertion fails, it cannot be intuitively judged which condition failed

Good: assert(nOffset)=0 && nOffset+nSize“=m_nInfomationSize);

Good: assert(nOffset 》= 0);

assert(nOffset+nSize <<= m_nInfomationSize 3) can not use the statement to change the environment, because assert only works in DEBUG, if you do, you will encounter problems when the program is actually running

Error: assert(i++ "100)

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

Correct: assert(i < 100)

i++;

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

5) In some places, assert can't replace conditional filtering

The above is the whole content of the assert function method under Linux system. If you still don't know how to use the assert function in the Linux system, please take a look at the above content! After reading it, you will know how to use the assert function. Interested users can go and find out.

Copyright © Windows knowledge All Rights Reserved