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运算符就是最常被引用的 ...
随机推荐
- Configure Red Hat Enterprise Linux shared disk cluster for SQL Server
下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) ...
- OmniGraffler软件和激活码
mac上用户画图的软件:OmniGraffler破解方法 1.激活码 Name: Appked SN: MFWG-GHEB-HYTW-CGHT-CSXU-QCNC-SXU 2.软件连接 链接: htt ...
- BNUOJ 19297 Code Refactoring
Code Refactoring Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...
- zoj2112 主席树动态第k大 (主席树&&树状数组)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 【bzoj4004】【JLOI2015】装备购买 (线性基+高斯消元)
Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 < ...
- 【Codeforces Round #505 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843966.html B: C:https://www.cnblogs.com/myx12345/p/9844084.ht ...
- ASP.NET2.0 ObjectDataSource的使用详解《转》
原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET2.0 ObjectDataSource的使用详解(2) http://mqingqing123.c ...
- [Python] 'unicode' object is not callable
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.
- 2017-10-28-afternoon-清北模拟赛
T1 水题(water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每张牌 ...
- Jetson TK1 五:移植工控机程序到板上
1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...