cassert

macro

assert

void assert (int expression);

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.

The specifics of the message shown depend on the particular library implementation, but it shall at least 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 as many assert calls as needed 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 the code, before the inclusion of <assert.h>.

Therefore, this macro is designed to capture programming errors, not user or run-time errors, since it is generally disabled after a program exits its debugging phase.

断言

void assert(int表达式);

评估断言

如果此宏的函数形式的参数表达式比较为零(即表达式为false),则向标准错误设备写入一条消息并调用abort,终止程序的执行。

显示的消息的细节取决于特定的库实现,但它至少应包括:断言失败的表达式、源文件的名称以及发生错误的行号。通常的表达式格式是:

断言失败:表达式,文件名,行号

如果在包含<assert.h>时,已定义名为NDEBUG的宏,则禁用此宏。这允许编码器在调试程序时根据需要在源代码中包含尽可能多的assert调用,然后通过简单地包含以下行来禁用生产版本的所有assert调用:

#定义NDEBUG

在代码的开头,在包含<assert.h>之前。

因此,此宏被设计用来捕获编程错误,而不是用户或运行时错误,因为它通常在程序退出调试阶段后被禁用。
eg:/* assert example /
#include <stdio.h> /
printf /
#include <assert.h> /
assert */

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 ©;

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值