C++和JAVA中有构造/析构函数,C语言中也有实现的方法,在gcc下可以使用关键字 __attribute__指定构造函数或者析构函数。他们由编译器在编译阶段进行处理。
- 声明构造函数:
void __attribute__((constructor)) function(void) - 声明析构函数:
void __attribute__((destructor)) function(void)
例:
#include <stdio.h>
void __attribute__((constructor)) init(void)
{
printf("constructor run...n");;
}
void __attribute__((destructor)) fini(void)
{
printf("destructor run...n");;
}
int main()
{
printf("Hello world!n");
return 0;
}
编译运行:
# gcc main.c
# ./a.out
constructor run...
Hello world!
destructor run...
最后
以上就是等待小海豚最近收集整理的关于C语言的构造函数与析构函数的全部内容,更多相关C语言内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复