exit():
在调用时,会做大部分清理工作,但是决不会销毁局部对象,因为没有stack unwinding。
会进行的清理工作包括:销毁所有static和global对象,清空所有缓冲区,关闭所有I/O通道。终止前会调用经由atexit()登录的函数,atexit如果抛出异常,则调用terminate()。
abort():
调用时,不进行任何清理工作。直接终止程序。
retrun:
调用时,进行stack unwinding,调用局部对象析构函数,清理局部对象。如果在main中,则之后再交由系统调用exit()。
示例:
(当然因为中华民族自墙不息之精神,还是转载源码比较好。。。)
by Alex
##############################################
############ 1.1 The foo_AAA.H file ##########
##############################################
#include <string>
#include <assert.h>
//----------
class AAA
{
private :
static int count_s;
int number_;
string name_;
public :
AAA ();
AAA (const string& name_i);
~AAA ();
##############################################
##############################################
############ 1.2 The foo_AAA.C file ##########
##############################################
#include "foo_AAA.H"
//----------
int AAA::count_s (0);
//----------
// Constructor-0
AAA::AAA ()
{
// Empty
//----------
// Constructor-1
AAA::AAA (const string& name_i)
{
name_ = name_i;
number_ = ++count_s;
cout << "Constructor called : Name#"
<< number_
<< " = "
<< name_
<< endl;
//----------
// Destructor
AAA::~AAA ()
{
cout << "Destructor called : Name#"
<< number_
<< " = "
<< name_
<< endl;
##############################################
##############################################
############ 1.3 The foo_main.C file #########
##############################################
#include "foo_AAA.H"
//----------
AAA obj1 ("global");
//----------
int main (int argc, char ** argv)
{
cout << "/tProgram " << argv [0] << " -> STARTED" << endl;
const string RETURN_termination = "RETURN";
const string EXIT_termination = "EXIT";
const string ABORT_termination = "ABORT";
if (!((argc == 2) &&
((argv [1] == RETURN_termination) ||
(argv [1] == EXIT_termination) ||
(argv [1] == ABORT_termination)
)
))
{
cout << "USAGE : "
<< argv [0]
<< " termination_type"
<< endl;
cout << " --- "
<< RETURN_termination
<< endl;
cout << " --- "
<< EXIT_termination
<< endl;
cout << " --- "
<< ABORT_termination
<< endl;
cout << "/tProgram "
<< argv [0]
<< " -> FATAL TERMINATION"
<< endl;
assert (0);
}
string termination_status = argv [1];
//==========================
AAA obj2 ("local_auto");
static AAA obj3 ("local_static");
//==========================
cout << "/tProgram "
<< argv [0]
<< " -> FINISHED : "
<< termination_status
<< endl;
if (termination_status == RETURN_termination)
{
return 0;
}
if (termination_status == EXIT_termination)
{
exit (0);
}
if (termination_status == ABORT_termination)
{
abort ();
}
assert (0);
##############################################
##############################################
############ 2.1 a.out RETURN ################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : RETURN
Destructor called : Name#2 = local_auto
Destructor called : Name#3 = local_static
Destructor called : Name#1 = global
//======== Conclusion =======
// All destructors are called
//===========================
##############################################
############ 2.2 a.out EXIT ##################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : EXIT
Destructor called : Name#3 = local_static
Destructor called : Name#1 = global
//======== Conclusion =======
// The local_auto object destructor isn't called
//===========================
##############################################
############ 2.3 a.out ABORT #################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : ABORT
//======== Conclusion =======
// No destructor is called
//===========================