【问题标题】:assert, -NDEBUG and segmentation fault断言、-NDEBUG 和分段错误
【发布时间】:2011-06-27 12:12:16
【问题描述】:

我有相当大的一段代码,在开发版本中运行良好,代码中有很多assert()。我使用传递给 g++ 的 -DNDEBUG 指令禁用了断言,现在我的代码因 seg 中断。过错。关于 assert() 有什么我不知道的吗?

【问题讨论】:

    标签: c++ c segmentation-fault assert


    【解决方案1】:

    据我所知,断言最常见的问题是在断言本身中有带有副作用的代码。当您使用 -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.
    

    【讨论】:

    • 要解决这个问题,对有副作用的代码使用 VERIFY 而不是 ASSERT,例如see here.
    猜你喜欢
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2023-03-22
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多