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运算符就是最常被引用的 ...
随机推荐
- 【PL/SQL编程基础】
[PL/SQL编程基础]语法: declare 声明部分,例如定义变量.常量.游标 begin 程序编写,SQL语句 exception 处理异常 end: / 正斜杠表示执行程序快范例 -- Cre ...
- tomcat的安装和优化
tomcat的安装 jdk版本安装 #!/bin/bash # desc: jdk安装脚本1. 1.7 1.8 download_url='http://**************' jdk_env ...
- 分析Tapjoy的模式—分发用于ios设备的企业级应用程序
下面简单介绍下Tapjoy的模式,供大家参考: Tapjoy最初的合作模式:“按安装奖励”(pay-per-install) Tapjoy利用非常成功的奖励性下载模式影响了App Store的免费游戏 ...
- 【Luogu】P2530化工厂装箱员(DP)
题目链接 不知道做出这道题是我能力的一个提升还是能力的回归. DP.设f[i][j][k][l]是已经取了i个产品,现在手里还拿着j件A,k件B,l件C,最小的操作数. 然后状转方程乱搞啊 #incl ...
- 【Luogu】P3708Koishi的数字游戏(数论)
题目链接 考虑f(i)=i%1+i%2+i%3+.....+i%n f(i+1)=(i+1)%1+(i+1)%2+......+(i+1)%n 其中不是i+1的因数的部分在f(i+1)的地方都加了1. ...
- 【图论】bnuoj 52810 Splitting the Empire
acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/G [题意] 给定一个无向图,要求把这个无向图的点划分到不同的集合里,使得每个集合的点之间两两没 ...
- Oracle命令行创建数据库
创建数据库文件 CREATE TABLESPACE MyDataBase LOGGING DATAFILE 'D:\Oracle\database\MyDataBase.dbf' SIZE 100M ...
- Xen虚拟化
Xen虚拟化基础 Xen虚拟化类型 hypervisor Xen组件 Xen hypervisor Colletion CPU.Memory.Interrupter Domain0 ---> D ...
- Codeforces 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- SpringBoot整合freemarker中自定义标签获取字典表的数据
因为在前端要根据字典表中的数据去将1.2这些值转换成对应的文字解释 1.首先要创建一个类去实现 TemplateDirectiveModel 类 @Component public class Dic ...