C Language Learning's assert

Source: Internet
Author: User

Tag: Indicates otherwise is the detail red its null pointer opens nod

C Language Learning's assert
    • ASSERT (programming terminology)
      When writing code, we always make assumptions that assertions are used to capture these assumptions in code, and assertions can be thought of as an advanced form of exception handling. Assertions are expressed as Boolean expressions, and programmers believe that the expression value is true at a particular point in the program. Assertion validation can be enabled and disabled at any time, so you can enable assertions while you are testing, and disable assertions at deployment time. Similarly, the end user can re-enable assertions when a problem is encountered after the program is put into operation.

    • Usage of assert in C language
      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 program execution if its condition returns an error.
      The detail of the assert is that the expression expr is evaluated first, and if its value is false (that is, 0) then it will print out the ASSERT content and __file__, line, __assert_function, and then execute abort () The function causes kernel to kill itself and coredump (whether to generate Coredump files, depending on the system configuration); otherwise, assert () has no effect.

    • Example 1 of the program listing ASSERT0CASE.C
      1) assert0case.c of the program
#include<assert.h>#include<stdio.h>#include<stdlib.h>struct ITEM{    int key;    int value;};/*add item to list,make sure list is not null*//*添加一个项目到列表中,列表不能为空*/void additem(struct ITEM* itemptr){    assert(itemptr!=NULL);    /*add item to list*/    /*添加项目之列表中,使用了assert*/}int main(void){    additem(NULL);    /*插入空指针,程序会报错*/    return 0;}

2) Results of execution

[[email protected] ~]$ gcc  ./assert0case.c -o assert0case[[email protected] ~]$ ./assert0caseassert0case: ./assert0case.c:12: additem: Assertion `itemptr!=((void *)0)‘ failed./*可以看到代码12行出现了问题,想插入NULL,但是列表不让*/Aborted (core dumped)
    • Example 2 of the program listing assert0case1.c
      1) assert0case1.c of the program
#include<stdio.h>#include<assert.h>#include<stdlib.h>int main(void){    FILE* fp;    fp=fopen("test.txt","w");//以可写的方式打开一个文件,如果不存在就创建一个同名文件    assert(fp);//所以这里不会出错    fclose(fp);    fp=fopen("noexitfile.txt","r");//以只读的方式打开一个文件,如果不存在就打开文件失败    assert(fp);//所以这里出错    fclose(fp);//程序永远都执行不到这里来    return 0;}

2) Results of execution

[[email protected] ~]$ gcc  ./assert0case1.c -o assert0case1[[email protected] ~]$ ./assert0case1assert0case1: ./assert0case1.c:10: main: Assertion `fp‘ failed./*可以看到代码第10行出现了问题,想打开noexitfile.txt的文件,但是没有,所以C语言报错*/Aborted (core dumped)

/*************************** Split Line ******************************/
For the purpose of the above experiment, look at the MySQL kernel source code. The following statements are used in many places.
The location of the source code is fut/fut0lst.cc found using the statement multiple times. ut_ad

/********************************************************************//**adds a node to an empty list.                    */staticvoidflst_add_to_empty (/*==============*/flst_base_node_t* Base,/*!< In:pointer to base node of Empty list */flst_node_t* node,/*!< in:node to add */mtr_t* mtr)/*!< in    : Mini-transaction handle */{ulint space;    fil_addr_t node_addr;    Ulint Len;    Ut_ad (MTR && Base && node);    Ut_ad (Base! = node);    /* This is used several times in the UT_AD statement, you can view the include/ut0dbg.h file */Ut_ad (Mtr_memo_contains_page (MTR, Base, Mtr_memo_page_x_fix));    Ut_ad (Mtr_memo_contains_page (MTR, node, mtr_memo_page_x_fix));    Len = Flst_get_len (base, MTR);    Ut_a (len = = 0);    BUF_PTR_GET_FSP_ADDR (node, &space, &AMP;NODE_ADDR);    /* Update First and last fields of base node */FLST_WRITE_ADDR (base + Flst_first, Node_addr, MTR);    FLST_WRITE_ADDR (base + flst_last, Node_addr, MTR); /* Set prev and next fields of nOde to add */FLST_WRITE_ADDR (node + flst_prev, Fil_addr_null, MTR);    FLST_WRITE_ADDR (node + flst_next, Fil_addr_null, MTR); /* Update Len of base node */Mlog_write_ulint (base + flst_len, Len + 1, mlog_4bytes, MTR);}

In parsing the include/ut0dbg.h file, the declaration of the UT_AD statement is found as follows:

#ifdef UNIV_INNOCHECKSUM#define ut_a        assert      /*全部都是assert断言*/#define ut_ad       assert#define ut_error    assert(0)#else /* !UNIV_INNOCHECKSUM */#ifdef UNIV_DEBUG/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */#define ut_ad(EXPR) ut_a(EXPR)/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */#define ut_d(EXPR)  do {EXPR;} while (0)#else/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */#define ut_ad(EXPR)/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */#define ut_d(EXPR)#endif

C Language Learning's assert

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.