function
<cstdlib>
atexit
int atexit (void (*func)(void));
extern "C" int atexit (void (*func)(void));
extern "C++" int atexit (void (*func)(void));
extern "C" int atexit (void (*func)(void)) noexcept;
extern "C++" int atexit (void (*func)(void)) noexcept;
Set function to be executed on exit
The function pointed by func is automatically called without arguments when the program terminates normally.
If more than one atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack (i.e. the last function specified is the first to be executed at exit).
A single function can be registered to be executed at exit more than once.
If atexit is called after exit, the call may or may not succeed depending on the particular system and library implementation (unspecified behavior).
If a function registered with atexit throws an exception for which it does not provide a handler when called on termination, terminate is automatically called (C++).
Particular library implementations may impose a limit on the number of functions call that can be registered with atexit, but this cannot be less than 32 function calls.
Parameters
- function
- Function to be called. The function shall return no value and take no arguments.
Return Value
A zero value is returned if the function was successfully registered.
If it failed, a non-zero value is returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
/* atexit example */
#include <stdio.h> /* puts */
#include <stdlib.h> /* atexit */
void fnExit1 (void)
{
puts ("Exit function 1.");
}
void fnExit2 (void)
{
puts ("Exit function 2.");
}
int main ()
{
atexit (fnExit1);
atexit (fnExit2);
puts ("Main function.");
return 0;
}
|
Output:
Main function.
Exit function 2.
Exit function 1.
|
Data races
Concurrently calling this function introduces no data races (C++): Calls are properly synchronized at the process level.
Exceptions (C++)
No-throw guarantee: this function never throws exceptions.
See also
- exit
- Terminate calling process (function
)
- abort
- Abort current process (function
)