When abort() is preferred over exit()?

。_饼干妹妹 提交于 2019-12-04 08:03:46

问题


I know the differences between the two. One notable thing is that abort() sends SIGABRT signal, so it may be relevant when your software relies on them. But for a typical application exit() seems to be more safe version of abort()...? Are there any other concerns to use abort() instead of exit()?


回答1:


Using abort will dump core, if the user has core dumps enabled. So as a rule of thumb, I'd use abort if you're so unsure about what's gone wrong that the only way to get useful information about it is by analysing a core dump.

If you can safely exit from any given point, and don't need the core dump, then exit is a nicer approach.




回答2:


Use abort() if your program is in a possibly corrupt state and you consider it too dangerous to try to do anything further. exit() will cause any atexit functions, and in C++ destructors of static objects, to be called. This is usually what you want for a clean exit, but it could be catastrophic if, for example, they overwrite a file with corrupt data.




回答3:


Sometimes your program breaks to such extent that its state becomes inconsistent and so exit() will not work because it would cause global objects destruction and the latter would not function properly when the state is inconsistent. In such situations abort() is to be preferred.




回答4:


Abort is preffered when application doesnot able to handle the exception and not able to understand what to do scenario. Exit() mean application should must finish all task gracefully. if exception occurs and application is able to handle the same then Exit() call happens.



来源:https://stackoverflow.com/questions/3676221/when-abort-is-preferred-over-exit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!