attribute constructor&destructor
attribute constructor&destructor
在看openwrt里libnl-tiny这个库的时候,遇到了C里面的构造函数这个概念。
static void __init init_msg_size(void)
{
default_msg_size = getpagesize();
}
这个static函数没有显示被其它地方调用,但确用了__init修饰。__init定义在include/netlink-local.h中:
#define __init __attribute__ ((constructor))
#define __exit __attribute__ ((destructor))
写一个测试函数
#include <stdio.h>
#define __init __attribute__ ((constructor))
#define __exit __attribute__ ((destructor))
static __init void before(void)
{
printf("before\n");
}
static __exit void after(void)
{
printf("after\n");
}
int main(void)
{
printf("main\n");
return 0;
}
打印结果为:
$ ./hello
before
main
after
还可以定义优先级:
#include <stdio.h>
#define __init101 __attribute__ ((constructor(101)))
#define __init102 __attribute__ ((constructor(102)))
#define __exit101 __attribute__ ((destructor(101)))
#define __exit102 __attribute__ ((destructor(102)))
static __init101 void before101(void)
{
printf("%s\n", __func__);
}
static __init102 void before102(void)
{
printf("%s\n", __func__);
}
static __exit101 void after101(void)
{
printf("%s\n", __func__);
}
static __exit102 void after102(void)
{
printf("%s\n", __func__);
}
int main(void)
{
printf("%s\n", __func__);
return 0;
}
打印结果为:
$ ./hello
before101
before102
main
after102
after101
优先级0~100被保留,自定义的优先级从101开始。
attribute constructor&destructor的更多相关文章
- error: expected constructor, destructor, or type conversion before '.' token
今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...
- 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客
[错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】
先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...
- 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...
- __attribute__中constructor和destructor
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- C之attribute用法
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和 ...
- attribute用法
attribute 用法 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了_ ...
- C++对象模型——Default Constructor的建构操作(第二章)
第2章 构造函数语意学 (The Semantics of Constructor) 关于C++,最常听到的一个抱怨就是,编译器背着程序猿做了太多事情.Conversion运算符就是最常被引用的 ...
随机推荐
- ubuntu上传项目到github
https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70544159
- 微软.net一些类的源码
地址:http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs 关键字:refer ...
- 【luogu】P1772物流运输(最短路+DP)
题目链接 对于本题我们设ext[i][j]计算第i个码头在前j天总共有几天不能用(其实就一前缀和),设dis[i][j]是从第i天到第j天不变运输路线的最短路径,设f[i]是前i天运输货物的最小花费. ...
- HDU——3786找出直系亲属(DFS+回溯)
找出直系亲属 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- [luoguP2805] [NOI2009]植物大战僵尸(网络流)
传送门 结论:这是最大权闭合图的模型 因为可能A保护B,B保护A,出现环. 所以由植物A向植物A保护的植物连边,然后拓扑排序,将环去掉. 然后将拓扑排序的边反向连,建立最大权闭合图的模型. 跑最大流( ...
- 【单调队列】bzoj 1407 [HAOI2007]理想的正方形
[题意] 给定一个n*m的矩阵,求所有大小为k*k的正方形中(最大值-最小值)的最小值 [思路] 先横着算出每一行的长度为k的窗口内的最大值,变成一个n*(m-k+1)的矩阵mx 再竖着算出每一列的长 ...
- 【DFS序+线段树区间更新区间求最值】HDU 5692 Snacks
http://acm.hdu.edu.cn/showproblem.php?pid=5692 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...
- Spoj-ODDDIV Odd Numbers of Divisors
Given a positive odd integer K and two positive integers low and high, determine how many integers b ...
- SpringBoot消失的Web.xml
Filter 过滤器作为web.xml中重要的一部分,有着相当高的出场率,SpringBoot会默认注册几个Filter ApplicationContextHeaderFilter Characte ...
- CentOS 7.5 初始网络配置
最近刚装完 CentOS 7.5 系统,由于网络不通,导致无法用 yum 命令下载软件,经过了各种折腾,终于搞定了,这里讲解一下 如何设置初始网络. 本案例环境 VmWare 11.0 , 操作系统 ...