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的更多相关文章

  1. error: expected constructor, destructor, or type conversion before '.' token

    今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...

  2. 【错误】expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客

    [错误]expected constructor, destructor, or type conversion before '.' token - 第八个游侠的日志 - 网易博客 [错误]expe ...

  3. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  4. 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】

    先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...

  5. 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】

    大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...

  6. __attribute__中constructor和destructor

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  7. C之attribute用法

    GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和 ...

  8. attribute用法

    attribute 用法 摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了_ ...

  9. C++对象模型——Default Constructor的建构操作(第二章)

    第2章    构造函数语意学 (The Semantics of Constructor) 关于C++,最常听到的一个抱怨就是,编译器背着程序猿做了太多事情.Conversion运算符就是最常被引用的 ...

随机推荐

  1. ci $this->load->database()

    http://pengbotao.cn/codeigniter-database.html

  2. jsp jstl标签库 el表达式

    一.JSTL标签是什么? 提供了对国际化(I18N)的支持,它可以根据发出请求的客户端地域的不同来显示不同的语言. 同时还提供了格式化数据和日期的方法.实现这些功能需要I18N格式标签库(I18N-c ...

  3. 一堆Offer怎么选?这样做就不纠结了

    有个朋友,工作了10年左右,春节后换工作,拿了三个Offer(西安): 通信行业的一家研究所,软件开发工程师,月薪7K,承诺有月奖金.年终奖金 一家做大数据的公司,软件开发工程师,月薪15K,13薪 ...

  4. ps 批量杀死进程

    ps aux | grep xxx |awk '{print $2}'|xargs kill -9

  5. P1140 相似基因 (动态规划)

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  6. 623. Add One Row to Tree

    Problem statement Given the root of a binary tree, then value v and depth d, you need to add a row o ...

  7. 在echars上发布的半圆环形图

    http://gallery.echartsjs.com/editor.html?c=xBJvoMcPfz&v=1

  8. pyserial安装

    参考网址:pyserial.sourceforge.net/pyserial.html#installation Download the archive from http://pypi.pytho ...

  9. 服务器Out of Memory

    之前有次把图片存储在数据库,结果读取时候报错了:Out of Memory.. 图片本来不应该存储在数据库中的,消耗内存太大,既然已经这样,那就先解决问题,不改存储方式. 如果一个应用程序为了提高性能 ...

  10. HTML5 这些你全知道吗?

    今天心情烦躁,搭建个论坛各种失败,Laravel.React也学不进去,于是就复习复习学过的.记录一下很少用的东西. 代码摘抄于W3C菜鸟教程HTML5手册 # 定义文本方向 <element ...