We often write large and complex programs. We allocate memory, open files/ resources and lots of other things. Most of the time our program executes in proper environment and encounters less/no error. We should handle this errors and exit properly.
Exiting from a logic or program involves freeing up memories and closing handles and files. We often write a cleanup handler which does this work. once cleanup is done we can exit gracefully. This cleanup ensures no memory and resource leak.
C runtime provides a atexit() API to register for cleanup handlers.
int atexit(void (*function)(void));
User need to implement these callbacks and C runtime will call all registered callbacks in reversed order. Maximum 32 routines can be registered for one application.
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.
Further readings
What is argc, argv in main function in c?[Explain]
Argc and argv are argument count and argument vector. Argc=number of arguments passed. Argv=Aarray of pointers, holds individual arguments. More..
Who calls main() function? What is C startup routine and assembly, explain?
Explains how control comes from C startup routine and assembly code to main routine. Setting up argc argv, environment variables before main
How does shell pass paramaters to my C program? [details of argv and environ]
How shell passes arguments and environment? Argc=number of arguments. Argv=Aarray of pointers, environ=Array of environment srtings. More..
How to create a child process with C library call system()?
How to create a child process in C? Talks about system() library function. Execute a shell command from your app. Check the status of child