c++中的断言
来源:互联网 发布:淘宝客url解密 编辑:程序博客网 时间:2024/06/09 17:41
assert(),即断言;
一般可以用于调试阶段的出错处理.
需要注意的是在release版本中,这个不起作用.
头文件是 assert.h(C), cassert(c++) .
示例:
//VC2005下编译通过.
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- void main()
- {
- char *p=NULL;
- char ch[50] = "this is a assert test in http://imzc.net ./n";
- assert(p!=NULL);
- printf("%s",ch);
- }
DEBUG下的提示:
Release下的提示:
下面是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
- C/C++ 中的assert()宏 断言机制
- C/C++ 中的assert()宏 断言机制
- C/C++ 中的assert()宏 断言机制 (转)
- 立此存照(13)[C++]C++中的assert断言使用示例
- C/C++ 中的assert()宏 断言机制 (转)
- C/C++assert 断言
- junit中的断言方法
- Java中的断言
- Visual C++ 中的断言
- Java中的断言问题
- c++中的断言
- java中的断言assert
- VC++中的ASSERT断言
- Junit 中的各种断言
- Swift中的断言assert
- gin中的断言写法
- Go语言中的<断言>
- IOS单元测试中的断言
- Java substring和substr用法
- 线程
- vc删除应用程序快捷方式
- asterisk 模拟卡常见问题以及解决办法(第一部分、第二部分)(转)
- vc添加应用程序快捷方式
- c++中的断言
- 半透明AlphaBlend
- linq to sql 学习(11)linq 构造更完美的动态数据查询(可扩展)
- 磨练
- FreePBX添加账户和密码的配置方法(转)
- 关于history.back()后,页面刷新的问题
- PulseEvent使用说明
- linux下c++的编译器g++的基本使用
- VC++ 调用 C#生成DLL的两种方法