In C, missing link between “Main process ends” to “call any functions registered with atexit”

StackOverflow https://stackoverflow.com/questions/1799306

  •  22-09-2019
  •  | 
  •  

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!

Was it helpful?

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 the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

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().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top