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运算符就是最常被引用的 ...
随机推荐
- Python第三方库之openpyxl(4)
Python第三方库之openpyxl(4) 2D柱状图 在柱状图中,值被绘制成水平条或竖列. 垂直.水平和堆叠柱状图. 注意:以下设置影响不同的图表类型 1.在垂直和水平条形图之间切换,分别设置为c ...
- Decorator(装饰器模式)
装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作. <?php class HtmlTemplate { // any parent class methods ...
- ZingChart 图表插件
ZingChart提供了一个丰富的API,用于通过重新绘制绘图(重新加载) ,加载新数据(setseriesdata),修改现有图表(modifyplot), 放大数据范围(zoomto),切换各种交 ...
- python类可以截获Python运算符
类可以截获Python运算符 现在,让我们来看类和模块的第三个主要差别: 运算符重载.简而言之,运算符重载就是让用类写成的对象,可截获并响应用在内置类型上的运算:加法.切片.打印和点号运算等.这只是自 ...
- 机房合作(三):We are Team,We are Family
导读:拖拖拉拉,机房的合作也算是接近了尾声了.在这个过程中,真心是感谢我的两个组员.这个机房合作,看似简单,但我的组员给我的帮助和感动,都是不可忽略的.记得刚开始的时候,我就说过:不怕猪一样的组长,咱 ...
- sql语句中的join连接(左连接、右连接、全连接、内连接)
内部连接(inner join): select * from d_user a inner join D_ORGANIZATION b on a.COMPANY_XID=b.ID 内部链接也是排他 ...
- 算法复习——欧拉函数(poj3090)
题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or e ...
- spring之Autowire
当使用spring为一个对象的属性注入时,通常使用xml文件的property元素和ref属性,但是我们也可以使用spring中的autowire进行注入 使用方法如下 <bean id=&qu ...
- Mybatis 如何自动生成bean dao xml 配置文件 generatorconfig.xml (main()方法自动生成更快捷)
最近项目要用到mybatis中间件,中间涉及到要对表结构生成bean,dao,和sqlconfig.xml 所以记录一下学习过程 首先是准备工作,即准备需要的jar包:我们的数据库mysql,所以驱动 ...
- java中执行JS脚本
package 测试包; import javax.script.*; public class SSSSSSSSS { public SSSSSSSSS() { // TODO Auto-gener ...