C++中的return, exit 与 abort

转自: http://blog.csdn.net/tangboyun/article/details/5288799


exit():

在调用时,会做大部分清理工作,但是决不会销毁局部对象,因为没有stack unwinding。

会进行的清理工作包括:销毁所有static和global对象,清空所有缓冲区,关闭所有I/O通道。终止前会调用经由atexit()登录的函数,atexit如果抛出异常,则调用terminate()。

 

abort():

调用时,不进行任何清理工作。直接终止程序。

 

retrun:

调用时,进行stack unwinding,调用局部对象析构函数,清理局部对象。如果在main中,则之后再交由系统调用exit()。

 

示例:

转自:gnu.gcc.help讨论组

(当然因为中华民族自墙不息之精神,还是转载源码比较好。。。)

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);

} // int main (int argc, char ** argv) 

##############################################

############################################## 
############ 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 
        //===========================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值