C++ exit() 和 abort() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/397075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between exit() and abort()?
提问by
In C and C++, what is the difference between exit()
and abort()
? I am trying to end my program after an error (not an exception).
在C和C ++,之间有什么区别exit()
和abort()
?我试图在出现错误后结束我的程序(不是例外)。
回答by Johannes Schaub - litb
abort()
exits your program without calling functions registered using atexit()
first, and without calling objects' destructors first. exit()
does both before exiting your program. It does not call destructors for automatic objects though. So
abort()
退出你的程序,而不先调用注册 using 的函数atexit()
,也不先调用对象的析构函数。exit()
在退出程序之前执行这两项操作。但是它不会为自动对象调用析构函数。所以
A a;
void test() {
static A b;
A c;
exit(0);
}
Will destruct a
and b
properly, but will not call destructors of c
. abort()
wouldn't call destructors of neither objects. As this is unfortunate, the C++ Standard describes an alternative mechanism which ensures properly termination:
会破坏a
和b
正确的,但不会调用析构函数c
。abort()
不会调用两个对象的析构函数。由于这是不幸的,C++ 标准描述了一种确保正确终止的替代机制:
Objects with automatic storage duration are all destroyed in a program whose function
main()
contains no automatic objects and executes the call toexit()
. Control can be transferred directly to such amain()
by throwing an exception that is caught inmain()
.
具有自动存储期的对象在其函数不
main()
包含自动对象并执行对exit()
.main()
通过抛出在 中捕获的异常,可以将控制直接转移到这样的main()
。
struct exit_exception {
int c;
exit_exception(int c):c(c) { }
};
int main() {
try {
// put all code in here
} catch(exit_exception& e) {
exit(e.c);
}
}
Instead of calling exit()
, arrange that code throw exit_exception(exit_code);
instead.
与其调用exit()
,不如安排该代码throw exit_exception(exit_code);
。
回答by Brian R. Bondy
abortsends a SIGABRT signal, exitjust closes the application performing normal cleanup.
abort发送一个 SIGABRT 信号,exit只是关闭执行正常清理的应用程序。
You can handle an abortsignal however you want, but the default behavior is to close the application as well with an error code.
您可以根据需要处理中止信号,但默认行为是关闭应用程序以及错误代码。
abortwill not perform object destruction of your static and global members, but exitwill.
abort不会对您的静态和全局成员执行对象销毁,但exit会。
Of course though when the application is completely closed the operating system will free up any unfreed memory and other resources.
当然,当应用程序完全关闭时,操作系统将释放任何未释放的内存和其他资源。
In both abortand exitprogram termination (assuming you didn't override the default behavior), the return code will be returned to the parent process that started your application.
在abort和exit程序终止(假设您没有覆盖默认行为)中,返回代码将返回到启动您的应用程序的父进程。
See the following example:
请参阅以下示例:
SomeClassType someobject;
void myProgramIsTerminating1(void)
{
cout<<"exit function 1"<<endl;
}
void myProgramIsTerminating2(void)
{
cout<<"exit function 2"<<endl;
}
int main(int argc, char**argv)
{
atexit (myProgramIsTerminating1);
atexit (myProgramIsTerminating2);
//abort();
return 0;
}
Comments:
注释:
If abortis uncommented: nothing is printed and the destructor of someobject will not be called.
If abortis commented like above: someobject destructor will be called you will get the following output:
如果取消注释abort:不打印任何内容并且不会调用某个对象的析构函数。
如果abort像上面这样注释:将调用 someobject 析构函数,您将获得以下输出:
exit function 2
exit function 1
退出函数 2
退出函数 1
回答by Robert Gamble
The following things happen when a program calls exit
():
程序调用exit
()时会发生以下情况:
- Functions registered by the
atexit
function are executed - All open streams are flushed and closed, files created by
tmpfile
are removed - The program terminates with the specified exit code to the host
- 函数注册的
atexit
函数被执行 - 所有打开的流都被刷新和关闭,创建的文件
tmpfile
被删除 - 程序以指定的退出代码终止到主机
The abort
() function sends the SIGABRT
signal to the current process, if it is not caught the program is terminated with no guarantee that open streams are flushed/closed or that temporary files created via tmpfile
are removed, atexit
registered functions are not called, and a non-zero exit status is returned to the host.
在abort
()函数发送SIGABRT
信号到电流过程中,如果它没有被捕获该程序终止于不能保证打开的流被刷新/闭合或经由创建的临时文件tmpfile
被移除,atexit
注册功能不叫,和非零退出状态返回给主机。
回答by Federico A. Ramponi
From the exit() manual page:
从 exit() 手册页:
The exit() function causes normal process termination and the value of status & 0377 is returned to the parent.
exit() 函数会导致正常的进程终止,并将 status & 0377 的值返回给父进程。
From the abort() manual page:
从 abort() 手册页:
The abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process. This results in the abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return.
abort() 首先解除对 SIGABRT 信号的阻塞,然后为调用进程引发该信号。这会导致进程异常终止,除非 SIGABRT 信号被捕获并且信号处理程序不返回。
回答by strager
abort
sends the SIGABRT
signal. abort
does not return to the caller. The default handler for the SIGABRT
signal closes the application. stdio
file streams are flushed, then closed. Destructors for C++ class instances are not, however (not sure on this one -- perhaps results are undefined?).
abort
发送SIGABRT
信号。 abort
不会返回给调用者。SIGABRT
信号的默认处理程序关闭应用程序。 stdio
文件流被刷新,然后关闭。然而,C++ 类实例的析构函数不是(不确定这个——也许结果是未定义的?)。
exit
has its own callbacks, set with atexit
. If callbacks are specified (or only one), they are called in the order reverse of their registration order (like a stack), then the program exits. As with abort
, exit
does not return to the caller. stdio
file streams are flushed, then closed. Also, destructors for C++ class instances are called.
exit
有自己的回调,设置为atexit
. 如果指定了回调(或仅指定了一个),则按照注册顺序的相反顺序(如堆栈)调用它们,然后程序退出。与abort
,exit
不会返回给调用者。 stdio
文件流被刷新,然后关闭。此外,还会调用 C++ 类实例的析构函数。