Assert () function usage Summary (recommended), assert Function

Source: Internet
Author: User

Assert () function usage Summary (recommended), assert Function

The assert macro prototype is defined in<assert.h>If its condition returns an error, terminate the program execution. The prototype is defined as follows:

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

The role of assert is to calculate the expression. If its value is false (that is, 0), It prints an error message to stderr and then calls abort to terminate the program running. See the following program list 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 writable mode, create a file with the same name as assert (fp) if it does not exist; // No error is found here: fclose (fp); fp = fopen ("noexitfile.txt", "r "); // open a file in read-only mode. If the file does not exist, assert (fp) fails to be opened. // The fclose (fp) error occurs here ); // The program will never be executed here to return 0 ;}
[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 having to stop using assert () Is that frequent calls will greatly affect program performance and increase additional overhead. After debugging, you can disable the assert call by inserting # define NDEBUG before the # include <assert. h> statement. The sample code is as follows:

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

Usage summary and precautions:

1) Check the validity of input parameters at the beginning of the function, for example:

Int resetBufferSize (int nNewSize) {// function: Change the buffer size, // parameter: New length of the nNewSize buffer // return value: current length of the buffer // description: nNewSize <= 0 indicates clearing the buffer assert (nNewSize> = 0); assert (nNewSize <= MAX_BUFFER_SIZE );...}

2) Each assert tests only one condition, because when multiple conditions are verified simultaneously, if the assert fails, you cannot intuitively determine which condition fails, for example:

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 takes effect only on DEBUG. If you do so, problems will occur when the program is actually running, such:

Error:

assert(i++ < 100);

This is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed.

Correct:

 assert(i < 100); i++;

4) The assert and subsequent statements should be empty in a row to form a logical and visual sense of consistency.

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

The above is a summary of the usage of the assert () function introduced by the editor. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.