In C, missing link between “Main process ends” to “call any functions registered with atexit”
Question
In C, when the main process ends -- how does it know to call any functions registered with atexit()
?
I understand how atexit()
works, but I don't understand the communication between "Main process ending" and "call any functions registered with atexit()
" I'm being a bit redundant.
Thanks!
Solution
From the C standard [PDF Link] (5.1.2.2.3):
a return from the initial call to the
main
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates the main function returns a value of0
.
It is the responsibility of the exit
function to call the functions registered with atexit
(see 7.20.4.3 in the standard for a description of everything that exit
does).
OTHER TIPS
In C, the main()
function is actually called by some other function, which is built into the runtime. This function, after the main()
function ends, does a few more things to clean up. One of them is to call any functions which have been registered with the atexit()
. This function actually stores some kind of static list of function pointers, which will be called by the runtime after main()
.