Bewerken

atexit

Processes the specified function at exit.

Syntax

int atexit(
   void (__cdecl *func )( void )
);

Parameters

func
Function to be called.

Return value

atexit returns 0 if successful, or a nonzero value if an error occurs.

Remarks

The atexit function gets the address of a function func to call when the program terminates normally. Successive calls to atexit create a register of functions that execute in last-in, first-out (LIFO) order. The functions passed to atexit can't take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions you can register is limited only by heap memory.

The code in the atexit function shouldn't contain any dependency on any DLL that could already be unloaded when the atexit function is called.

Microsoft-specific DLL behavior: When a DLL unloads, after DllMain receives DLL_PROCESS_DETACH, the DLL's atexit callbacks run in reverse registration order, with the last callback registered running first.

To generate an ANSI-conformant application, use the ANSI-standard atexit function (rather than the similar _onexit function).

Requirements

Routine Required header
atexit <stdlib.h>

Example

This program pushes four functions onto the stack of functions to execute when atexit is called. When the program exits, it executes these functions in last-in, first-out order.

// crt_atexit.c
#include <stdlib.h>
#include <stdio.h>

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

int main( void )
{
   atexit( fn1 );
   atexit( fn2 );
   atexit( fn3 );
   atexit( fn4 );
   printf( "This is executed first.\n" );
}

void fn1()
{
   printf( "next.\n" );
}

void fn2()
{
   printf( "executed " );
}

void fn3()
{
   printf( "is " );
}

void fn4()
{
   printf( "This " );
}
This is executed first.
This is executed next.

See also

Process and environment control
abort
exit, _Exit, _exit
_onexit, _onexit_m