当前位置:首页 > 芯闻号 > 充电吧
[导读]__attribute__: GNU C 的一大特色就是__attribute__ 机制。__attribute__ 可以设置函数属性(Function),变量属性(Variable)和类型属性(Ty


__attribute__: GNU C 的一大特色就是__attribute__ 机制。__attribute__ 可以设置函数属性(Function),变量属性(Variable)和类型属性(Type)。

__attribute__ 书写特征是:

---- __attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数。

__attribute__ 语法格式为:__attribute__ ((attribute-list))

__attribute__关键字主要是用来在函数或数据声明中设置其属性。给函数设置属性的主要目的在于让编译器进行优化。GCC为函数提供了几种类型的属性,其中包含:

1)构造函数(constructors)和析构函数(destructors)。

__attribute__((constructor)): 需要在main函数之前调用的函数,可以设置constructor属性,实现初始化。

2)packed属性:使用该属性可以使得变量或者结构体成员使用最小的对齐方式。

__attribute__((packed))的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。这个功能和OS没有关系,和编译器有关,gcc的编译器不是紧凑模式的,在windows下,vc的编译器也不是紧凑的,tc的编译器是紧凑的。例如:

在GCC下: 

struct data{
	char ch; 
	int num;
};

sizeof(int) = 4; sizeof(data) = 8; (非紧凑模式)

struct data{
	char ch; 
	int num;
}__attribute__((packed));

sizeof(int) = 4; sizeof(data) = 5;

程序员应当使用类似下面的方式来指定这些属性:


__attribute__((constructor)) static void beforeFunction()
{
    printf("beforeFunctionn");
}

查阅GNU的文档:



constructor ()destructor ()


--- The constructor attribute causes the function to be called automatically before execution enters main().

--- Similarly, the destructor attribute causes the function to be called automatically after main() completes or exit() is called.

--- Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.You can set an optional integer priority to control the order in which constructor and destructor funcs are run.  A constructorwith a smaller priority number runs before a constructor with a larger priority number;the opposite relationship holds for destructors. 构造函数优先级数字越小,越先执行。析构函数相反。


So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority.  

The priorities for constructor and destructor functions are the same as those specified for namespace-scope

C++ objects (see C++ Attributes).These attributes are not currently implemented for Objective-C. 


/* max length */
#define MAX_LEN 16
/*server max number */
#define MAX_SNUM 10
/*port max number*/
#define MAX_PNUM 10
enum server_state{idle = 0,in_use};

//server information
typedef struct server_info{
    char host_name[MAX_LEN];
	int port_id[MAX_PNUM];
	enum server_state st[MAX_PNUM];
	char used_by[MAX_PNUM][MAX_LEN];
	char ip_address[MAX_PNUM][MAX_LEN];
} S_Info;

char pre_host_name[MAX_SNUM][MAX_LEN] = {
                    "eslruntime01",
                    "eslruntime02",
                    "eslruntime03",
                    "eslruntime04",
                    "eslruntime05",
                    "eslruntime06",
                    "eslruntime07",
                    "eslruntime08",
                    "eslruntime09",
                    "eslruntime10"
};

int port_inf[MAX_PNUM] = {10000,10001,10002,10003,10004,10005,10006,10007,10008,10009};
enum server_state sst[MAX_PNUM] = {idle};
//create server table
static S_Info * init_server_info()
{
    S_Info * server_t = (S_Info *)malloc(sizeof(S_Info) * MAX_SNUM);
    int i,j,k;
    for (i = 0;i < MAX_SNUM;++i)
	{ 
	    for(j = 0;j < MAX_PNUM;++j)
		{
            server_t[i].port_id[j] = port_inf[j];
            server_t[i].st[j] = sst[j];
			memset(server_t[i].used_by[j],0,MAX_LEN);
			memset(server_t[i].ip_address[j],0,MAX_LEN);
		}
	    for(k = 0;k < MAX_LEN;++k)
		{
	        server_t[i].host_name[k] = pre_host_name[i][k];		    
	    }			
	}
    return server_t;
}

//global variable
S_Info * server_total;
static void create_total_server() __attribute__((constructor));
static void free_total_server() __attribute__((destructor));

static void create_total_server()
{
	server_total = init_server_info();
}

static void free_total_server()
{
	free(server_total);
	server_total = NULL;
}


可以给属性设置优先级:



static  __attribute__((constructor(101))) void before1()
{

    printf("before1n");
}
static  __attribute__((constructor(102))) void before2()
{

    printf("before2n");
}
static  __attribute__((constructor(103))) void before3()
{

    printf("before3n");
}


以上三个函数会依照优先级的顺序调用.括号内的数值(101,102,103)代表优先级,另外,我以前看过,这个1-100的范围是保留的.

所以,最好从100之后开始用.(但是实际上,我在项目中测试100以内的,也没有得到警告)

main函数之前的,即constructor的优先级,数值越小,越先调用。destructor中的数值越大,越先调用。

关于格式 -- 按照文档中的说法,__attribute__应该放在函数声明的尾部;之前.


__attribute__((constructor)) // 在main函数被调用之前调用
__attribute__((destructor)) // 在main函数被调用之后调
#include__attribute__((constructor)) void before_main() { 
   printf("before mainn"); 
} 

__attribute__((destructor)) void after_main() { 
   printf("after mainn"); 
} 

int main(int argc, char **argv) { 
   printf("in mainn"); 
   return 0; 
}
//这个例子的输出结果将会是:
before main
in main
after main


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

其实在 c++语言里面const修饰的才算是一个真正的常量,在 c 语言中 const 可以说是个“冒牌货”。为什么会这样?其实是 c++ 编译器对 const 进行了加强,当 c++ 编译器遇到常量声明时,不会像 c...

关键字: c++ C语言 const

返回函数的引用去初始化一个新的引用这个和前面一样,都是不会产生副本,但是现在是用返回值去初始化一个引用声明c,也就是说这时候变成了变量temp的别名,在c的生命周期内temp是一直有效的,这样做完全可以。

关键字: c++ 返回值 引用声明

C++是一种面向对象的高级程序设计语言,是C语言的超集。

关键字: c++ C语言

众所周知,在java里是不能给构造函数写返回值的,如果在低版本的编译器定义一个构造器写上返回值可能会报错,高版本里面他就是一个普通的方法。可如果构造函数没有返回值,比如Test t = new Test()我们new一个...

关键字: 构造函数 java

分析:这是Adobe 公司2007 年校园招聘的最新笔试题。这道题除了考察应聘者的C++ 基本功底外,还能考察反应能力,是一道很好的题目。 在Java 中定义了关键字final ,被final 修饰的

关键字: c++ class

泛型算法中的定制操作很多算法都会比较输入序列中的元素,通过定制比较动作,可以控制算法按照编程者的意图工作。本文以string排序为例进行说明,首先是缺省的排序动作: vector v{"This","

关键字: c++

为什么是lambda?讲了这么多天的lambda表达式,有一个很基本的问题没有回答:为什么叫lambda表达式呢?首先这个lambda就是罗马字母λ,lambda表达式即λ表达式。数学上有一个概念叫λ

关键字: c++

        假设我们有个函数用来揭示处理程序的优先权,另一个函数用来在某动态分配所得的Widget 上进行某些带有优先权的处理:int priority () ; void processWi

关键字: c++ effective

判断链表中是否有环最经典的方法就是快慢指针,同时也是面试官大多想要得到的答案。       快指针pf(f就是fast的缩写)每次移动2个节点,慢指针ps(s为slow的缩写)每次移动1个节点,如果快

关键字: c++ 链表 快慢指针

转载请注明出处:http://blog.csdn.net/callon_h/article/details/52073268 引子 上一篇博客从内核驱动到android app讲述了android通过

关键字: c++ java
关闭
关闭