C + + assert () function Usage Summary

Source: Internet
Author: User
Tags assert prototype definition

1. Introduction

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

Prototype definition:

<assert.h>
voidint expression);

The function of an assert is to evaluate the expression first, if its value is false (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort. Take a look at the following program listing BADPTR.C:

#include<Stdio.h>
#include<Assert.h>
#include<Stdlib.h>
IntMainvoid)
{
FILE*fp

Fp=fopen"Test.txt","W");//Open a file in a writable manner and create a file with the same name if it does not exist
ASSERT (FP);//So there's no mistake here.
Fclose (FP);

Fp=fopen "noexitfile.txt" "r "); //
assert (FP); // So here's the error
fclose (FP); // The program never executes here
return 0}

[Email protected] error_process]# gcc badptr.c
[Email protected] error_process]#./a.out
A.out:badptr.c:14:main:assertion ' FP ' failed.

The use of ASSERT () has been abandoned because frequent calls can greatly affect the performance of the program and add additional overhead. After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:

<stdio.h>
#define Ndebug
<assert.h>

2. Usage Summary and Precautions:

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

 int Resetbuffersize ( int Nnewsize) 
{
// function: Change the buffer size,
// parameters: Nnewsize buffer New length
// return value: Buffer current length
// Description: Keep the contents of the original information intact nnewsize<=0 means clear buffer
Span style= "COLOR: #000000" > assert (nnewsize >= 0);  
Assert (nnewsize <= max_buffer_size);
...
} /span>

  

2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed, such as:

Not good:

ASSERT (Noffset>=0&& noffset+nSize<=m_ninfomationsize);    

Good:

>=0);
ASSERT (Noffset+<= m_ninfomationsize);

3) You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into problems when it is actually running, such as:

Error:

ASSERT (I++<);   

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

That's right:

<);
I+ +;

4) The Assert and subsequent statements should be empty lines to form a logical and visual sense of consistency.

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

Assert is used to avoid obvious errors, not to handle exceptions. Errors and exceptions are not the same, errors should not occur, exceptions are unavoidable. C language anomalies can be handled by conditional judgment, and other languages have their own exception handling mechanisms.

A very simple rule of using Assert is that at the very beginning of a method or function, if you use it in the middle of the method, you need to consider carefully whether it should be. At the beginning of the method, a functional process was not started, and the problems that occurred during the execution of a functional process were almost abnormal.

C + + assert () function Usage Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.