退出(0)与返回 0

exit(0) vs return 0(退出(0)与返回 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

基础教程推荐