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

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

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

std::exit(0); 向系统返回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()相似但不调用析构函数。

猜你喜欢

转载自blog.csdn.net/kenfan1647/article/details/114273708