c - 如何使用 atexit() 函数来清理函数调用?

标签 c exit atexit

<分区>

我在我的代码中使用 atexit() 函数来清理函数调用,但它不起作用。

#include<stdio.h>
void ftn(void)
{
    printf(" Function called --> exit\n");
    return;
}
int main(void)
{
    int x = 0;
    atexit(ftn);
    for(;x<0xffffff;x++);
    _exit(0);
}

如有任何帮助,我们将不胜感激。

最佳答案

atexit() 函数的这种行为是由于使用了函数 _exit()。此函数不会调用 atexit() 等清理函数。如果需要调用 atexit(),则应使用 exit() 或“return”,而不是 _exit()。

作为:

#include<stdio.h>
void ftn(void)
{
    printf(" Function called --> exit\n");
    return;
}
int main(void)
{
    int x = 0;
    atexit(ftn);
    for(;x<0xffffff;x++);
    exit(0);
}

关于c - 如何使用 atexit() 函数来清理函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355008/

相关文章:

c - 该程序集的等效 C 代码是什么?

c - 使用C编写wav文件时的量化噪声

bash - 基于 bash 中循环操作的带有错误代码的退出脚本

我可以撤消或删除 atexit 命令吗?

python - 发生未处理的异常时如何跳过 sys.exitfunc

c - 使用 epoll 的多线程 UDP 服务器?

c - 如果电源关闭,调试源代码时会发生什么

iPhone UIApplicationExitsOnSuspend 无效

c# - 应用程序退出代码始终返回 0

c++ - 当我们使用 atexit() 时,不会为本地对象调用析构函数