exit(0) vs return 0

前端 未结 1 2022
旧巷少年郎
旧巷少年郎 2021-02-08 13:33

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

相关标签:
1条回答
  • 2021-02-08 13:52

    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.

    0 讨论(0)
提交回复
热议问题