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
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
