gpt4 book ai didi

c++ - atexit() 函数仿生与 glibc

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:55:27 28 4
gpt4 key购买 nike

我发现了一个有趣的时刻:atexit() 函数对于 bionicglibc 的工作方式不同。这是一个例子:

#include <cstdlib>
#include <cstdio>
extern "C" {
void one(){
printf("one\n");
}
void two() {
printf("two\n");
atexit(one);
}
}
int main() {
atexit(two);
}

bionic 的结果:

two

glibc 的结果:

two
one

为什么结果不同?

最佳答案

It is unspecified whether a call to the atexit function that does not happen before the exit function is called will succeed.

ISO C 标准,§7.22.4.2。所以这两种行为都是合规的;当 exit 已经在运行其 atexit 处理程序时,您无法使用 atexit 可靠地注册函数。

关于c++ - atexit() 函数仿生与 glibc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030273/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com