【问题标题】:Is null terminate() handler allowed?是否允许 null terminate() 处理程序?
【发布时间】:2010-02-09 19:29:20
【问题描述】:

在 VC++7 中,如果我执行以下操作:

void myTerminate()
{
    cout << "In myTerminate()";
    abort();
}

int main( int, char** )
{
    set_terminate( &myTerminate );
    set_terminate( 0 );
    terminate();
    return 0;
}

程序的行为与直接调用 abort() 完全相同,这正是默认的 terminate() 处理程序所做的。

如果我省略 set_terminate( 0 ); 语句,我的终止处理程序将被调用。所以调用set_terminate( 0 ) 似乎有将terminate() 处理程序重置为默认值的效果。

这种行为是否仅适用于 VC++7?如果我在其他一些实现上调用set_terminate( 0 ),程序会不会遇到未定义的行为?

【问题讨论】:

    标签: c++ visual-c++ terminate undefined-behavior


    【解决方案1】:

    查看该标准会发现以下内容:

    terminate_handler set_terminate(terminate_handler f) throw();
    

    1 作用:建立功能 由 f 指定为当前处理程序 function ... cut
    2 要求:f 不得为空指针
    3 返回:前一个 terminate_handler。

    好像不规范。

    【讨论】:

    • 所以这是未指定的行为,不是吗?
    • 是的,这是未定义的行为。您需要存储以前的处理程序并显式恢复它。
    • @Alexander Gessler:准确地说,“未指定的行为”!=“未定义的行为”,因此“不,这是未定义的行为”。
    • (不同的是,在可能的行为范围有限的情况下使用“未指定的行为”。有些程序可以容忍任何可能的行为,因此未指定的行为不一定是坏的。未定义的行为永远不能被容忍)。
    • @MSalters - 有趣的区别,标准是否以这些术语表示?
    猜你喜欢
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2018-07-12
    • 2012-07-25
    • 1970-01-01
    • 2019-01-25
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多