c++标准库中的程序终止

1、std::exit() 由系统调用,表示结束进程。对于已构造的对象会调用析构函数(在一个析构函数中调用exit()会无限递归)。

  • std::exit(0); 向系统返回0,表示程序正常结束。
  • std::exit(1); 向系统返回1,表示程序非正常结束。

2、std::abort() 程序异常结束,不会调用析构函数:

#define debug qDebug()<<
struct ceshi
{
    ~ceshi()
    {
        debug "ceshi 析构";
    }
};

int main(int argc, char *argv[])
{
    ceshi c;
    std::abort();
    debug "hello";
}

3、std::atexit(void (*fun) (void)) 参数是一个函数指针,指向一个无参数无返回值的函数,终止前可以执行制定的函数。会调用析构函数,析构顺序:在std::atexit()前面定义的后析构,在std::atexit()后面定义的先析构:

#define debug qDebug()<<
struct ceshi
{
    ~ceshi()
    {
        debug "ceshi 析构" << a;
    }
    int a;
};

void debugValue()
{
    debug "hello world";
}

int main(int argc, char *argv[])
{
    ceshi c1;
    c1.a = 88;
    std::atexit(debugValue);
    ceshi c2;
    c2.a = 66;

    debug "hello";
}

4、std::quick_exit() 和exit()相似但不调用析构函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值