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

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

  •  22-09-2019
  •  | 
  •  

문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top