//#define NDEBUG //禁用assert()函数
#include <iostream>
#include <cassert>
using namespace std;
/*
* 函数名:assert()
* 原型:void assert( int expression )
* 功能:如果形参为假则终止程序
* 参数:真或假 注意:每个assert只能检查一个条件,如果多个条件不好判断是哪个条件的错误
* 具体:如果形参数为假,assert 向 stderr打印一条出错信息,
* 信息包含文件名、表达式、行号,然后调用abort终止程序
* 如果形参为真,程序继续执行
* 优点:可以方便我们进行程序调试,同时对于绝对不能出错(条件为假)的地方使用可以有效的预防出现更多的错误
* 缺点:assert是宏函数,频繁的调用会增加额外的开销,影响程序性能
* 禁用assert函数:在#include前面加上#define NDEBUG
*/
void print_number(int* myInt){
assert(myInt!=NULL); //如果myInt!=NULL,程序继续执行,反之程序打印报错信息,程序终止运行
//assert(myInt!=NULL && 1==0) //检查多个条件
cout<<*myInt<<endl;
}
int main(void)
{
int a=10;
int* b = NULL;
int* c = NULL;
b = &a;
print_number(b);
print_number(c);
return 0;
}
iotek@iotekclass:~/pwz1903C++/Depa/面试题/方凌计算机$ ./a.out
10
a.out: test6.cpp:18: void print_number(int*): Assertion `myInt!=__null' failed.
已放弃 (核心已转储)