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运算符就是最常被引用的 ...
随机推荐
- Leetcode 397.整数替换
整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...
- pytorch保存模型等相关参数,利用torch.save(),以及读取保存之后的文件
本文分为两部分,第一部分讲如何保存模型参数,优化器参数等等,第二部分则讲如何读取. 假设网络为model = Net(), optimizer = optim.Adam(model.parameter ...
- 2014 ACM/ICPC Asia Regional 北京 Online
G - Grade Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushroo ...
- system sys,sysoper sysdba 的区别
--===================================== -- system sys,sysoper sysdba 的区别 --========================= ...
- ASP.NET中一般处理程序报的错误:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值
1.把context.Response.End();代码换成 HttpContext.Current.ApplicationInstance.CompleteRequest(); 2.把context ...
- iOS-runtime-根据协议名调某一个类有与协议里面放的相同的方法
// // ViewController.m // ObserverTrampoline // // Created by Rob Napier on 9/7/11. // Copyright (c) ...
- Python之注册表增删改查(干货)
在Windows平台下,对注册表的增删改查的需求比较多,微软提供了很多用于访问,修改注册表等的API,我们可以使用诸如bat,或者C++等各种方式去访问修改注册表.无所不能的python下如何完成这些 ...
- BZOJ-1269 文本编辑器
.... 这道题就是Noi原题嘛...虽然更容易写... 题意: 建立一个数据结构,并支持以下操作: Insert 区间插入有序序列:Delete 区间删除:Rotate 区间翻转:Get 单点查询 ...
- poj 2115 二元一次不定方程
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14765 Accepted: 3719 Descr ...
- 家用电脑架服务器提供web
要搞一个可以对外的web服务,需要服务器,域名.这些都需要money,但有时,我们只是想自己可以在外面访问,或是提供给朋友看自己的网站有多牛.这时使用家用电脑配置一个可以提供web的服务器,就显得很必 ...