Assert
来源:互联网 发布:大数据和数据挖掘教程 编辑:程序博客网 时间:2024/05/18 08:39
assert宏的原型定义在< assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:
#include < assert.h> void assert( int expression );
C++ assert()函数的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
/* assert example */#include <stdio.h>#include <assert.h>void print_number(int* myInt) { assert (myInt!=NULL); printf ("%d\n",*myInt);}int main (){ int a=10; int * b = NULL; int * c = NULL; b=&a; print_number (b); print_number (c); return 0;}
In this example, assert is used to abort the program execution if print_number is called with a null pointer as attribute. This happens on the second call to the function, which triggers an assertion failure to signal the bug.
使用C++ assert()函数的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。 在调试结束后,可以通过在包含#include < assert.h>的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include < stdio.h> #define NDEBUG #include < assert.h>
- assert
- assert
- assert
- assert
- ASSERT()
- assert
- assert
- ASSERT
- ASSERT
- assert
- assert
- assert
- ASSERT
- assert
- ASSERT
- assert
- assert
- assert()
- 理解inode
- 讣告:传智播客创始人张孝祥老师年前意外辞世
- 上下文切换
- HQL检索方式、QBC检索方式、SQL检索方式
- C语言中的面向对象思想
- Assert
- 2012.1.2 poj2346 自己独立想出状态转移方程的DP题目
- 黑马程序员----看视频后对反射总结做的总结
- 1
- opencv Iplimage data access
- 配置Ubuntu 10.10提供HTTPS服务
- 动态规划 杭电1231
- sql server 触发器详解
- TLD 算法问题:libopencv_calib3d.so.2.2: cannot open shared object file: No such file or directory