__attribute__((constructor))

__attribute__((constructor))

The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.

构造函数属性使函数在执行进入main()之前自动被调用。同样,析构函数属性使函数在main()完成或调用exit()之后自动被调用。具有这些属性的函数对于初始化在程序执行期间隐式使用的数据很有用。

就是指在函数上方加上__attribute__((constructor))可以让这个函数在main函数执行前运行

#include<stdio.h>


__attribute__((constructor))
void print(){
    printf("This is print func!\n");
    return ;
}

int main(){
    printf("This is main func!\n");
    return 0;
}

输出
在这里插入图片描述
可以看到print函数在main函数之前运行了
__attribute__((constructor))可以提前初始化一些在main函数中用到的东西
如果你了解AVL树,可以看一下其代码中NIL节点的初始化例子
https://blog.csdn.net/moX980/article/details/107561605

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙diao网友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值