Analysis of the difference between the end function exit _exit atexit

  • 2020-04-02 01:30:27
  • OfStack

More time in the end of the program we need to do some operations such as release resources, but the program exits, there are many ways of such as the main () function ends, somewhere in the program with the exit () the end of the program, the user through a Ctrl + C or Ctrl + break operation to terminate the program, etc., so you need to have a has nothing to do with the program exits mode method to carry out the necessary processing when the program exits. The method USES the atexit() function to register the function to be called when the program terminates normally.

The argument to the atexit() function is a function pointer to a function that takes no arguments and returns no value. The function prototype of atexit() is: int atexit(void (*)(void));

Up to 32 handlers can be registered in a program with atexit(), which is called in the reverse order of its registration, that is, the last call first registered and the first call last registered.


#include <stdlib.h>
#include <stdio.h>
void fun()
{
    printf("fun/n");
}
int main()
{
    atexit(fun);
    printf("hello/n");
    return 0;
}
// int atexit(void (*func)()) //  see  <stdlib.h>  Defined in the 
// {
//     func();
//     return 0;
// }

The above code will output

Hello!

fun

After removing the red annotation code, the library function is redefined due to the Interpositioning behavior, so that atexit is only a common function

So the output


fun 
hello
#include <stdlib.h>
#define EXIT_FAILURE ...
#define EXIT_SUCCESS ...
void exit (int status);
void _Exit(int status);          //C99
void abort(void);
int atexit(void (*func)(void)));

The exit, _Exit, and abort functions abort the program, and control does not return to the caller of these functions.

Function exit normally terminates the program and performs the following cleanup operations:

1. (to standard C language) all functions registered with the atexit function are called in the reverse order as they were registered.

2. The output stream is opened by refreshing, and all open data streams are closed.

3. Delete the file generated by the tepfile function.

Control returns to the host environment, providing status values.

As is the custom on many systems, a status value of 0 indicates successful termination and a non-zero value indicates abnormal termination. The value of 0 and the macro EXIT_SCCESS in standard C indicates successful termination, the value of the macro EXIT_FAILURE indicates unsuccessful termination, and the meaning of the other values is defined by the implementation. Returning an integer value from main is equivalent to calling the exit function with this value.

The function _Exit differs from the exit function in that it calls neither the exit handler registered with atexit nor the signal handler registered with singal. Whether or not to perform other cleanup operations is defined by the implementation, such as closing all open data streams. _Exit is an addition to C99, and some implementations traditionally provide similar functionality with a function called _Exit.

The abort function aborts a program and does not call a function registered with atexit. Whether abort causes a cleanup operation is defined by the implementation, and the state value returned to the host system is also defined by the implementation, but should be expressed as unsuccessful. In standard C and many traditional implementations, abort is called to turn into a special signal that can be captured (SIGABRT in standard C). If the signal is ignored or the handler returns, the standard C implementation still terminates the program, while other implementations may cause the abort function to return to the caller. Abort is also called when an assertion fails.

The atexit function, added to the standard C language, registers a function that is called when exit is called or when the function main returns. When a program terminates abnormally (such as abort or raise), the registered function is not called. The implementation should allow at least 32 functions to be registered. If the registration is successful, the atexit function returns 0, otherwise it returns a non-0 value and the function cannot log out. All functions registered with the atexit function are called in the reverse order of registration, and the atexit function performs all standard cleanup operations. Each function is called without arguments and should have the return type void. The registration function cannot refer to any object whose storage class is auto or register that is not defined by itself (for example, by pointer reference). A function registered a few times will be called a few times at this point.

Pointer function using example:


#include<stdlib.h>
#include<stdio.h>
typedef void (*pFunc)(float a);
// int atexit(void (*func)())
// {
//     func();
//     return 0;
// }
int atexitf( void (*func)(float),float a)
{
    func(a);
    return 0;
}
void test(float a)
{
     printf("test %f/n",a);
}
// void fun()
// {
//     printf("fun/n");
// }
int main()
{
    pFunc pFunc1 = (pFunc)test;  //Function pointer assignment
    pFunc1(4.5);

    atexitf(pFunc1,3.66);
    atexitf(test,3.66);
    //atexitf(test(4.3));
    printf("hello/n");
    getchar();
    return 0;
}


Related articles: