c++中的断言

assert(),即断言;
一般可以用于调试阶段的出错处理.
需要注意的是在release版本中,这个不起作用.
头文件是 assert.h(C), cassert(c++) .

示例:

 //VC2005下编译通过.

C++代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <assert.h>  
  4.   
  5. void main()  
  6. {  
  7.    char *p=NULL;  
  8.    char ch[50] = "this is a assert test in http://imzc.net ./n";   
  9.    assert(p!=NULL);  
  10.    printf("%s",ch);  
  11. }  

DEBUG下的提示:

 

大小: 27.13 K
尺寸: 400 x 191
浏览: 13 次
点击打开新窗口浏览全图

Release下的提示:

大小: 5.59 K
尺寸: 400 x 106
浏览: 13 次
点击打开新窗口浏览全图

 


下面是C++标准上面的定义,

来自http://www.cplusplus.com/reference/clibrary/cassert/assert/.

void assert (int expression);
<cassert>

Evaluate assertion

If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false), a message is written to the standard error device and abort is called, terminating the program execution.

[如果说表达式expression 的值为0,将错误信息写到标准错误设备,然后abort()终止函数调用.]

The specifics of the message shown depend on the specific implementation in the compiler, but it shall include: the expression whose assertion failed, the name of the source file, and the line number where it happened. A usual expression format is:
[错误信息格式为:]
Assertion failed: expression, file filename, line line number

This macro is disabled if at the moment of including assert.h a macro with the name NDEBUG has already been defined. This allows for a coder to include many assert calls in a source code while debugging the program and then disable all of them for the production version by simply including a line like:

#define NDEBUG 

at the beginning of its code, before the inclusion of assert.h.

Parameters

expression
Expression to be evaluated. If this expression evaluates to  0, this causes an  assertion failure that terminates the program.

Return Value

none

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值