简体   繁体   English

在C语言中,“主进程结束”与“调用在atexit中注册的任何函数”之间缺少链接

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

In C, when the main process ends -- how does it know to call any functions registered with atexit() ? 在C中,当主进程结束时-如何知道调用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. 我了解atexit()工作原理,但不了解“主进程结束”与“调用在atexit()注册的任何函数”之间的通信,我有点多余。

Thanks! 谢谢!

In C, the main() function is actually called by some other function, which is built into the runtime. 在C语言中, main()函数实际上是由运行时内置的其他函数调用的。 This function, after the main() function ends, does a few more things to clean up. main()函数结束之后,此函数还需要进行其他一些清理工作。 One of them is to call any functions which have been registered with the atexit() . 其中之一是调用已在atexit()注册的任何函数。 This function actually stores some kind of static list of function pointers, which will be called by the runtime after main() . 此函数实际上存储某种类型的函数指针的静态列表,运行时将在main()之后调用它们。

From the C standard [PDF Link] (5.1.2.2.3): C标准 [PDF链接](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; 从初始调用到main函数的返回等同于以main函数返回的值作为参数来调用exit函数; reaching the } that terminates the main function returns a value of 0 . 到达终止主函数的}会返回值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). 它是有责任exit函数来调用注册的功能atexit (见7.20.4.3的标准,一切的描述exit一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM