assert.h(断言)

来源:互联网 发布:全国城市经纬度数据库 编辑:程序博客网 时间:2024/05/22 06:11

断言类似于异常处理。

#include<assert.h>#include<stdio.h>#include<stdlib.h>struct ITEM{int key;int value;};void addItem(struct ITEM *itemptr){assert(itemptr!=NULL);}int main(){addItem(NULL);return 0;}


 

比如这个程序。在主函数中添加NULL,当执行到assert(null)的时候,它首先想stderr(貌似标准错误格式流)打印一条出错信息,然后通过absort来终止

禁用assert

#define NDEBUG

#include <assert.h>

这样就可以禁用断言。