📜  C / C++中的atexit()函数

📅  最后修改于: 2021-05-30 16:41:14             🧑  作者: Mango

当程序正常终止时,由atexit()指向的函数将在不带参数的情况下自动调用。如果多于一个的函数已经被不同的呼叫到的atexit()函数指定,所有的都在堆叠的顺序执行(即指定的最后一个函数是在出口处将要执行的第一个)。可以注册一个函数以在退出处执行一次以上。
句法 :

extern "C" int atexit (void (*func)(void)) noexcept;
extern "C++" int atexit (void (*func)(void)) noexcept

注意: extern表示名称将指向整个程序中的同一对象。
参数:该函数接受单个强制性参数func ,该函数指定在正常程序终止时要调用的函数的指针(要调用的函数)。
返回值:该函数返回以下值:

  • 零,如果函数注册成功
  • 非零,如果函数注册失败

下面的程序说明了上述函数:
程序1:

CPP
// C++ program to illustrate
// atexit() function
#include 
using namespace std;
 
// Returns no value, and takes nothing as a parameter
void done()
{
    cout << "Exiting Successfully"
         << "\n"; // Executed second
}
// Driver Code
int main()
{
    int value;
    value = atexit(done);
 
    if (value != 0) {
        cout << "atexit () function registration failed";
        exit(1);
    }
    cout << " Registration successful"
         << "\n"; // Executed First
    return 0;
}


CPP
// C++ program to illustrate
// more than one atexit function
#include 
using namespace std;
 
// Executed last, in a Reverse manner
void first()
{
    cout << "Exit first" << endl;
}
 
// Executed third
void second()
{
    cout << "Exit Second" << endl;
}
 
// Executed Second
void third()
{
    cout << "Exit Third" << endl;
}
 
// Executed first
void fourth()
{
    cout << "Exit Fourth" << endl;
}
// Driver Code
int main()
{
    int value1, value2, value3, value4;
    value1 = atexit(first);
    value2 = atexit(second);
    value3 = atexit(third);
    value4 = atexit(fourth);
    if ((value1 != 0) or (value2 != 0) or
        (value3 != 0) or (value4 != 0)) {
        cout << "atexit() function registration Failed" << endl;
        exit(1);
    }
    // Executed at the starting
    cout << "Registration successful" << endl;
    return 0;
}


CPP
// C++ program to illustrate
// atexit() function when it throws an exception.
#include 
using namespace std;
 
void shows_Exception()
{
    int y = 50, z = 0;
    // Program will terminate here
    int x = y / z;
 
    // Cannot get printed as the program
    // has terminated
    cout << "Divided by zero";
}
// Driver Code
int main()
{
    int value;
    value = atexit(shows_Exception);
    if (value != 0) {
        cout << "atexit() function registration failed";
        exit(1);
    }
 
    // Executed at the starting
    cout << "Registration successful" << endl;
    return 0;
}


输出:
Registration successful
Exiting Successfully

如果多次调用atexit函数,则所有指定的函数将以相反的方式执行,与堆栈的功能相同。
程式2:

CPP

// C++ program to illustrate
// more than one atexit function
#include 
using namespace std;
 
// Executed last, in a Reverse manner
void first()
{
    cout << "Exit first" << endl;
}
 
// Executed third
void second()
{
    cout << "Exit Second" << endl;
}
 
// Executed Second
void third()
{
    cout << "Exit Third" << endl;
}
 
// Executed first
void fourth()
{
    cout << "Exit Fourth" << endl;
}
// Driver Code
int main()
{
    int value1, value2, value3, value4;
    value1 = atexit(first);
    value2 = atexit(second);
    value3 = atexit(third);
    value4 = atexit(fourth);
    if ((value1 != 0) or (value2 != 0) or
        (value3 != 0) or (value4 != 0)) {
        cout << "atexit() function registration Failed" << endl;
        exit(1);
    }
    // Executed at the starting
    cout << "Registration successful" << endl;
    return 0;
}
输出:
Registration successful
Exit Fourth
Exit Third
Exit Second
Exit first

程序3:

CPP

// C++ program to illustrate
// atexit() function when it throws an exception.
#include 
using namespace std;
 
void shows_Exception()
{
    int y = 50, z = 0;
    // Program will terminate here
    int x = y / z;
 
    // Cannot get printed as the program
    // has terminated
    cout << "Divided by zero";
}
// Driver Code
int main()
{
    int value;
    value = atexit(shows_Exception);
    if (value != 0) {
        cout << "atexit() function registration failed";
        exit(1);
    }
 
    // Executed at the starting
    cout << "Registration successful" << endl;
    return 0;
}

注意:如果已注册的函数引发无法处理的异常,则将调用Terminate()函数。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。