注册终止函数atexit的作用
2012-06-21 10:47
375 查看
很多时候我们需要在程序退出的时候做一些诸如释放资源的操作,但程序退出的方式有很多种,比如main()函数运行结束、在程序的某个地方用exit() 结束程序、用户通过Ctrl+C或Ctrl+break操作来终止程序等等,因此需要有一种与程序退出方式无关的方法来进行程序退出时的必要处理。方法就是用atexit()函数来注册程序正常终止时要被调用的函数。
atexit()函数的参数是一个函数指针,函数指针指向一个没有参数也没有返回值的函数。atexit()的函数原型是:int atexit (void (*)(void));
在一个程序中最多可以用atexit()注册32个处理函数,这些处理函数的调用顺序与其注册的顺序相反,也即最先注册的最后调用,最后注册的最先调用。
#include <stdlib.h>
#include <stdio.h>
void fun()
{
printf("fun\n");
}
int main()
{
atexit(fun);
printf("hello\n");
return 0;
}
// int atexit(void (*func)()) //
见 <stdlib.h> 中定义
// {
// func();
// return 0;
// }
上面的代码将输出
hello
fun
而把红色的注释代码去掉之后,由于Interpositioning行为,重定义了库函数,使atexit仅仅表现为一个普通的函数
因此输出
fun
hello
atexit()函数的参数是一个函数指针,函数指针指向一个没有参数也没有返回值的函数。atexit()的函数原型是:int atexit (void (*)(void));
在一个程序中最多可以用atexit()注册32个处理函数,这些处理函数的调用顺序与其注册的顺序相反,也即最先注册的最后调用,最后注册的最先调用。
#include <stdlib.h>
#include <stdio.h>
void fun()
{
printf("fun\n");
}
int main()
{
atexit(fun);
printf("hello\n");
return 0;
}
// int atexit(void (*func)()) //
见 <stdlib.h> 中定义
// {
// func();
// return 0;
// }
上面的代码将输出
hello
fun
而把红色的注释代码去掉之后,由于Interpositioning行为,重定义了库函数,使atexit仅仅表现为一个普通的函数
因此输出
fun
hello
相关文章推荐
- atexit注册终止函数
- exit 终止一个进程 atexit 注册终止函数
- atexit()注册终止函数和t权限
- atexit()函数来注册程序正常终止时被调用的函数
- atexit()注册终止函数
- 注册终止函数atexit
- atexit函数:注册终止处理程序
- atexit()函数 -- 注册终止函数
- Linux - atexit()(注册终止)函数
- atexit注册进程终止处理函数
- atexit()函数,进程终止,关于suid,sgid和粘滞位
- 注册终止处理函数atexit()
- 【Linux】进程终止以及atexit()函数的介绍
- atexit函数的作用
- platform_device_系列函数及其设备注册的作用
- 程序结束后调用的函数 注册终止函数
- atexit注册的函数是在main函数之后执行?
- 注册终止函数
- atexit()函数调用注册函数的顺序
- 终止函数 atexit()