C++ standard library program termination

1. std::exit() is called by the system to indicate the end of the process. The destructor is called for the constructed object (calling exit() in a destructor will recurse infinitely).

std::exit(0); Returns 0 to the system, indicating that the program ends normally.

std::exit(0); Returns 1 to the system, indicating that the program ends abnormally.

2. The std::abort() program ends abnormally, and the destructor will not be called:

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

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

3. The parameter of std::atexit(void (*fun) (void)) is a function pointer, which points to a function with no parameters and no return value. The specified function can be executed before termination. The destructor will be called, and the order of destruction is: the post-destructor defined before std::atexit(), and the first destructor defined after 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() is similar to exit() but does not call the destructor.

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/114273708