Assert in the C language

Source: Internet
Author: User
Tags prototype definition

  1. 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.
    Library functions: Assert.h
    Prototype definition: void assert (int expression);
    The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.

  2. Routines:
    #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 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");//Open a file as read-only and fail to open if it does not exist
    ASSERT (FP); So there's a mistake.
    Fclose (FP); The program will never be executed 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.
    has been abandoned
    The disadvantage of using assert is that 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:
    #include <stdio.h>
    #define Ndebug
    #include <assert.h>

3. Macro Name: Assert
Function: Test a condition and possibly terminate the program
Usage: void assert (int test);
program Example:
Copy CodeThe code is as follows:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct ITEM {
int key;
int value;
};

/* Add item to list, make sure list was not NULL */
void AddItem (struct ITEM *itemptr) {
ASSERT (Itemptr! = NULL);
/* Add Item to list */
}

int main (void)
{
AddItem (NULL);
return 0;
}

4.assert () macro usage
Note: Assert is a macro, not a function. In the assert.h header file of C.
The prototype of an Assert macro is defined in <assert.h>, and its function is to terminate the execution of the program if its condition returns an error, and the prototype defines:
Copy CodeThe code is as follows:
#include <assert.h>
void assert (int expression);

the function of an assert is to evaluate an expression firstexpressionif the value is False (that is, 0), then it first flows to the standard errorstderrprints an error message and then calls theAbortto terminate the program; otherwise, assert () has no effect. Macro assert () is generally used to confirm the normal operation of the program, where the expression construction is not a mistake for truth. After debugging is complete, you do not have to remove the ASSERT () statement from the source code because the macro ndebug definition is empty. 5.Usage Summary and precautions:1) Verify the legitimacy of incoming parameters at the beginning of the function
  1. Such as:

    int resetbuffersize (int nnewsize)
    {
    Function: Change buffer size,
    Parameter: Nnewsize buffer new length
    Return value: Buffer current length
    Note: Keep the original information content unchanged nnewsize<=0 means clear buffer
    ASSERT (nnewsize >= 0);
    ASSERT (Nnewsize <= max_buffer_size);
    ...
    }

    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
    Bad: Assert (noffset>=0 && noffset+nsize<=m_ninfomationsize);
    Good: Assert (noffset >= 0);
    ASSERT (Noffset+nsize <= 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
    Error: Assert (i++ < 100)
    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.
    Correct: Assert (I < 100)
    i++;
    4) Assert and subsequent statements should be empty lines to create a logical and visual sense of consistency

    5) In some places, assert cannot replace conditional filtering

Assert in the C language

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.