【发布时间】:2011-06-27 12:12:16
【问题描述】:
我有相当大的一段代码,在开发版本中运行良好,代码中有很多assert()。我使用传递给 g++ 的 -DNDEBUG 指令禁用了断言,现在我的代码因 seg 中断。过错。关于 assert() 有什么我不知道的吗?
【问题讨论】:
标签: c++ c segmentation-fault assert
我有相当大的一段代码,在开发版本中运行良好,代码中有很多assert()。我使用传递给 g++ 的 -DNDEBUG 指令禁用了断言,现在我的代码因 seg 中断。过错。关于 assert() 有什么我不知道的吗?
【问题讨论】:
标签: c++ c segmentation-fault assert
据我所知,断言最常见的问题是在断言本身中有带有副作用的代码。当您使用 -DNDEBUG 编译时,断言基本上被注释掉了,因此断言中的代码不会被执行。断言手册页在错误部分中提到了这一点:
BUGS
assert() is implemented as a macro; if the expression tested has side-
effects, program behavior will be different depending on whether NDEBUG
is defined. This may create Heisenbugs which go away when debugging is
turned on.
【讨论】: