Possible Duplicate:
what is the purpose of atexit function?
In UNIX at least: I’m aware that C/C++ can register a number of functions to be called at the exit of main – exit handlers. Thee functions to be called can be registered, in reverse order, using:
int atexit(void (*func) (void));
I’m having trouble determining how this would be useful though. The functions are void/void and global, so they are unlikely to have access to many variables around the program unless the variables are also globals. Can someone let me know the kinds of things you would do with exit handlers?
Also, do exit handlers work the same way on non-UNIX platforms since they’re part of an ANSI C specification?
You can perform cleanup for global objects in a
atexithandler:If you want to be able to close over some variables in an atexit-like handler, you can devise your own data structure containing cleanup function/parameter pairs, and register a single atexit handler calling all the said functions with their corresponding arguments.