exit(0) vs return 0(退出(0)与返回 0)
问题描述
当 exit(0) 用于退出程序时,本地的析构函数范围内的非静态对象不被调用.但是析构函数是如果使用 return 0 则调用.注意静态对象将是即使我们调用 exit() 也会清理干净.
When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.Note that static objects will be cleaned up even if we call exit().
这个逻辑背后应该有一些原因.我只是想知道它是什么?谢谢.
There should be some reason behind this logic. i just want to know what it is? Thank you.
推荐答案
在 exit( 0 )
的情况下,您正在调用一个函数.你不要期望调用局部变量的析构函数 if你正在调用一个函数.编译器不知道,先验地,exit(0)
有什么特别之处.
In the case of exit( 0 )
, you're calling a function. You
don't expect the destructors of local variables to be called if
you're calling a function. And the compiler doesn't know,
a priori, that there is anything special about exit( 0 )
.
事实上,这个原理实际上只适用于之前的 C++例外.该标准可以重新定义 exit()
来抛出一个实现用参数定义异常,并指定对 main
的调用被包装在一个 try 块中,该块捕获此异常,并将返回代码传递回系统.这意味着 exit
有一个完全不同的然而,C 和 C++ 中的语义;无论如何,没有提交委员会的提案以进行此更改.
In fact, this rationale really only applies to C++ before
exceptions. The standard could redefine exit()
to throw an
implementation defined exception with the argument, and specify
that the call to main
is wrapped in a try block which catches
this exception, and passes the return code back to the system.
This would mean that exit
have a completely different
semantics in C and in C++, however; at any rate, there's been no
proposal before the committee to make this change.
这篇关于退出(0)与返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:退出(0)与返回 0


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30