return语句VS在主要出口()() [英] return statement vs exit() in main()
问题描述
我应该使用退出()
或只是
?我个人推荐使用收益
语句的main()收益
语句,因为我觉得它像阅读其它任何功能,当我在看code中的流量控制是平滑的(在我看来)。即使我想重构的main()
函数,有收益
好像不是<$ C是更好的选择$ C>退出()。
Should I use exit()
or just return
statements in main()
? Personally I favor the return
statements because I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to refactor the main()
function, having return
seems like a better choice than exit()
.
确实退出()
做什么特别的收益
不?
推荐答案
其实,有是的区别,但它的微妙。它有C ++更多的影响,但差异是很重要的。
Actually, there is a difference, but it's subtle. It has more implications for C++, but the differences are important.
当我称之为收益
在的main()
,析构函数将被称为我的本地范围对象。如果我称之为退出()
,没有析构函数将被调用为我在本地范围内的对象!重新读取。 退出()
不返回。这意味着,一旦我把它称为有无backsies。你已经在该函数创建的任何对象,也不会被破坏。通常,这并没有影响,但有时它像关闭文件(当然你要所有的数据刷新到磁盘?)。
When I call return
in main()
, destructors will be called for my locally scoped objects. If I call exit()
, no destructor will be called for my locally scoped objects! Re-read that. exit()
does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all your data flushed to disk?).
注意静态
的对象将被清理,即使你叫退出()
。最后注意,如果你使用中止()
,没有对象将被销毁。也就是说,没有全球性的对象,无静电的对象和没有本地对象将呼吁他们的析构函数。
Note that static
objects will be cleaned up even if you call exit()
. Finally note, that if you use abort()
, no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.
偏袒返回退出时谨慎行事。
<一个href=\"http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a\">http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a
这篇关于return语句VS在主要出口()()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!