我有一段相当大的代码,在开发版本中工作得很好,代码中有很多assert()。我用传递给g++的-DNDEBUG指令禁用了断言,现在我的代码用seg中断了。过错。关于assert()有什么我不知道的吗?
发布于 2011-06-27 20:15:56
据我所知,assert最常见的问题是在assert本身中具有副作用的代码。当您使用-DNDEBUG编译时,断言基本上被注释掉了,因此断言中的代码不会被执行。assert手册页在bugs部分提到了这一点:
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.https://stackoverflow.com/questions/6492668
复制相似问题