exit(0)vs return 0 [英] exit(0) vs return 0

查看:129
本文介绍了exit(0)vs return 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当exit(0)用于退出程序时,不会调用本地
scoped非静态对象的析构函数。但是析构函数是
,如果使用返回0。注意静态对象​​将
清理,即使我们调用exit()。


这个逻辑应该有一些原因。我只想知道它是什么? c>退出(0), 你正在调用一个函数。你
不期望调用局部变量的析构函数,如果
你正在调用一个函数。编译器不知道,
先验,有任何特殊的 exit(0)



事实上,这个理由真的只适用于C ++之前的
异常。该标准可以重新定义 exit()以使用参数抛出
实现定义的异常,并指定
调用 main 被包装在一个try块中,它捕获
这个异常,并将返回的代码传递回系统。
这意味着 exit 在C和C ++中有一个完全不同的
语义;无论如何,在委员会之前没有
的提案来进行此更改。


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.

解决方案

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 ).

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.

这篇关于exit(0)vs return 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆