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.
Have you encountered such a situation, the server is running normally, the number of users accessing
We all know that DeVeDe can make videos and files into video DVDs. How should DeVeD
The network management of Red Hat Enterprise Linux 7 and CentOS 7 is actually the management of Netw
Under Linux, you need to implement file transfer between two servers. You can use t
Steps to Add Static Routes to Linux
Linux system file permissions and access mode introduction
Install the deb package operation instance on Ubuntu
How to solve the omitting directory error when copying files in Linux?
What shortcuts do Linux systems use with Visual Studio?
How to solve the problem of cant find libjava.so under Ubuntu?
Domestic system Linux Deepin 2014 detailed evaluation
Summary of methods for configuring ip address under Linux system
How to write uppercase numbers from one to ten? How do you write the numbers from one to ten?
How to install SQL Server 2005 under Windows 7
How to make good use of Windows7 self-repair function
How to deal with WinXP playing games all the way back to the desktop?
How to adjust the order of input methods under win8.1 system
How to upgrade free win10 Win10 official version free upgrade using skills summary
Win8 computer installation application hangs how to do
Excess WindowsVista boot menu after booting, how to delete
Can Win10 upgrade for free? Win10 free upgrade extension method