An analysis of ending program function exit, _exit,atexit difference _c language

Source: Internet
Author: User
Tags function prototype terminates

Many times we need to do something like release resources when the program exits, but there are many ways to exit the program, such as the end of the main () function, the end of the program at some point in the application, the user through the CTRL + C or Ctrl+break operation to terminate the program and so on, Therefore, it is necessary to have a method unrelated to the way the program exits in order to handle the process when the program exits. The method is to use the atexit () function to register the function to be called when the program terminates normally.

The parameter of the Atexit () function is a function pointer to a function that has no parameters and no return value. The function prototype of atexit () is: int atexit (void (*) (void));

In a program, you can register up to 32 processing functions with atexit (), which are called in the reverse order of their registration, the last call that was first registered, and the first call that was last registered.

Copy Code code as follows:

#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> definition
// {
Func ();
return 0;
// }


the code above will output

Hello

Fun

After the red annotation code is removed, because of the interpositioning behavior, the library function is redefined so that the atexit only behaves as a normal function

So the output

Copy Code code as follows:

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 terminate the program, and control does not return to the caller of those functions.

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

1. (Into the standard C language) all functions that want to register functions are called in the reverse order of registration, and several times are atexit.

2. Refresh opens the output stream and closes all open data streams.

3. Delete the file generated by the Tepfile function.

4. Control returns the host environment, provides the status value.

According to the custom in many systems, a status value of 0 indicates that the terminating program was successful and that the exception was terminated with a value other than 0. A value of 0 and a macro exit_sccess in standard C indicates a successful termination, and the value of the macro exit_failure indicates that the termination was unsuccessful and that the other values were defined by the implementation. Returning an integer value from function 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 Atexit registered exit processor nor the Singal registered signal processor. Whether to perform additional cleanup operations is defined by the implementation, such as closing all open data streams. _exit are C99 added, and some implementations traditionally provide similar functionality with functions called _exit.

The Abort function terminates the program abnormally and does not invoke functions registered to the atexit. Abort whether the cleanup operation is caused by an implementation definition, the state value returned to the host system is also defined by the implementation, but should be expressed as "unsuccessful." In standard C language and many traditional implementations, the call abort is converted into a special signal that can be captured (standard C language is SIGABRT). If the signal is ignored or the processor returns, the standard C language implementation still terminates the program, while other implementations may cause the Abort function to return to the caller. The assertion failure also invokes abort.

The Atexit function is added in the standard C language, which registers a function that calls the exit or function main when it returns. The registered function is not invoked when the program terminates abnormally, such as by abort or raise. Implementation should allow at least 32 functions to be registered. If the registration succeeds, the Atexit function returns 0, otherwise the function cannot be logged out unless it returns a value other than 0. All functions registered to the ATEXIT function are called in the reverse order of registration, and then all standard cleanup operations are performed by the atexit function. Each function is called without a parameter, and should have return type void. The registration function cannot refer to any object that is not defined as the auto or register of the storage class (for example, by reference to a pointer). A few times the function registration will be called at this time.

Example of using pointer functions:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>

typedef void (*pfunc) (float a);

//int atexit (void (*func) ())
//{
//     func ();
     return 0;
//}

Int ATEXITF (*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
    pfun C1 (4.5);
/*    atexit (fun); */
    ATEXITF (pfunc1,3.66);
    ATEXITF (test,3.66);
   //atexitf (Test (4.3));
    printf ("hello/n");
    GetChar ();
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.