资料来自:
http://blog.sina.com.cn/s/blog_7e719f0501012tkt.html
http://www.cnblogs.com/astwish/p/3460618.html
http://bbs.youkuaiyun.com/topics/390275079
GNU C的一大特色就是__attribute__机制。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。 __attribute__关键字主要是用来在函数或数据声明中设置其属性。给函数赋给属性的主要目的在于让编译器进行优化。
__attribute__书写特征是:__attribute__前后都有两个下划线,并且后面会紧跟一对括弧,括弧里面是相应的__attribute__参数。语法格式为:
__attribute__ ((attribute-list))
其位置约束:放于声明的尾部“;”之前。
noreturn
函数声明中的__attribute__((noreturn)),就是告诉编译器这个函数不会返回给调用者,以便编译器在优化时去掉不必要的函数返回代码。如:
void myexit() __attribute__((noreturn));
C库函数中的abort()和exit()的声明格式就采用了这种格式。
extern void myexit();
int test(int n)
{
if ( n > 0 )
{
myexit();
/* 程序不可能到达这里*/
}
else
return 0;
}
直接编译,会有警告输出
$gcc –Wall –c noreturn.c
noreturn.c: In function `test':
noreturn.c:12: warning: control reaches end of non-void function
myexit修改为:
extern void myexit() __attribute__((noreturn));
警告就没了
const
__attribute__((const))属性只能用于带有数值类型参数的函数上。当重复调用带有数值参数的函数时,由于返回值是相同的,所以此时编译器可以进行优化处理,除第一次需要运算外, 其它只需要返回第一次的结果就可以了,进而可以提高效率。该属性主要适用于没有静态状态(static state)和副作用的一些函数,并且返回值仅仅依赖输入的参数。
例如:
// 用gcc -o const const.c后运行const, inc(10)就会输出两次。
// 用gcc -O -o const const.c后运行const, inc(10)只会输出一次。
#include <stdio.h>
int __attribute__((const)) inc(int x)
{
printf("%s(%d)\n", __FUNCTION__, x);
return x + 1;
}
int inc2(int x)
{
printf("%s(%d)\n", __FUNCTION__, x);
return x + 1;
}
int main(void)
{
int i, j;
i = inc(10);
j = inc(10);
printf("%d %d\n", i, j);
i = inc2(10);
j = inc2(10);
printf("%d %d\n", i, j);
return 0;
}
通过添加attribute((const))声明,编译器只调用了函数一次,以后只是直接得到了相同的一个返回值。
事实上,const参数不能用在带有指针类型参数的函数中,因为该属性不但影响函数的参数值,同样也影响到了参数指向的数据,它可能会对代码本身产生严重甚至是不可恢复的严重后果。
并且,带有该属性的函数不能有任何副作用或者是静态的状态,所以,类似getchar()或time()的函数是不适合使用该属性的。
constructor/destructor
若函数被设定为constructor属性,则该函数会在main()函数执行之前被自动的执行。类似的,若函数被设定为destructor属性,则该 函数会在main()函数执行之后或者exit()被调用后被自动的执行。拥有此类属性的函数经常隐式的用在程序的初始化数据方面。
例如一个不一样的hello world
#include <stdio.h>
#include <stdlib.h>
static __attribute__((constructor)) void before()
{
printf("Hello");
}
static __attribute__((destructor)) void after()
{
printf(" World!\n");
}
int main(int args,char ** argv)
{
return EXIT_SUCCESS;
}
输出:Hello World!
该属性的函数执行时可指定优先级,格式为
__attribute__((constructor(PRIORITY)))
__attribute__((destructor(PRIORITY)))
其中PRIORITY的取值需大于100,因为0到100为实现保留,并且默认时启用的
举个栗子:
#include <stdio.h>
#include <stdlib.h>
__attribute((constructor(101))) void pre_func_101(void)
{
printf("pre_func_101\n");
}
__attribute((constructor(102))) void pre_func_102(void)
{
printf("pre_func_102\n");
}
__attribute((destructor(101))) void post_func_101(void)
{
printf("post_func_101\n");
}
__attribute((destructor(102))) void post_func_102(void)
{
printf("post_func_102\n");
}
int main()
{
return EXIT_SUCCESS;
}
输出为
pre_func_101
pre_func_102
post_func_102
post_func_102
可见,constructor的优先级与destructor是相反的。
Function Attribute
函数属性(Function Attribute):函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。__attribute__机制也很容易同非GNU应用程序做到兼容之功效。函数属性可以给被声明的函数加上类似printf或者scanf的特征,它可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配。
GNU CC需要使用 –Wall编译器来击活该功能,这是控制警告信息的一个很好的方式。
format的语法格式为:
format (archetype, string-index, first-to-check)
format属性告诉编译器,按照printf, scanf, strftime或strfmon的参数表格式规则对该函数的参数进行检查。
- “archetype”指定是哪种风格;
- “string-index”指定传入函数的第几个参数是格式化字符串;
- “first-to-check”指定从函数的第几个参数开始按上述规则进行检查。
具体使用格式如下:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
其中参数m与n的含义为:
- m:第几个参数为格式化字符串(format string)(从1开始计数)
- n:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几(从1开始计数)
//m=1;n=2
extern void myprint(const char *format,...) __attribute__((format(printf,1,2)));
//m=2;n=3
extern void myprint(int l,const char *format,...)
__attribute__((format(printf,2,3)));
- 注意,如果myprint是一个类的非静态成员函数,那么m和n的值不一样了,例如:
//m=3;n=4
extern void myprint(int l,const char *format,...)
__attribute__((format(printf,3,4)));
其原因是,类的非静态成员函数的第一个参数实际上一个“隐身”的“this”指针。
Packed属性
packed属性:使用该属性可以使得变量或者结构体成员使用最小的对齐方式,即对变量是一字节对齐,对域(field)是位对齐。
如果你看过GPSR协议在TinyOS中的实现,你一定会注意到下面的语句:
typedef struct {
double x;
double y;
} __attribute__((packed)) position_t;
packed是类型属性(Type Attribute)的一个参数,使用packed可以减小对象占用的空间。需要注意的是,attribute属性的效力与你的连接器也有关,如果你的连接器最大只支持16字节对齐,那么你此时定义32字节对齐也是无济于事的。
使用该属性对struct或者union类型进行定义,设定其类型的每一个变量的内存约束。当用在enum类型定义时,暗示了应该使用最小完整的类型(it indicates that the smallest integral type should be used)。
下面的例子中,my-packed-struct类型的变量数组中的值会紧凑在一起,但内部的成员变量s不会被“pack”,如果希望内部的成员变量也被packed的话,my-unpacked-struct也需要使用packed进行相应的约束。
struct my_unpacked_struct
{
char c;
int i;
};
struct my_packed_struct
{
char c;
int i;
struct my_unpacked_struct s;
}__attribute__ ((__packed__));
在每个系统上看下这个结构体的长度吧。
内存对齐,往往是由编译器来做的,如果你使用的是gcc,可以在定义变量时,添加attribute,来决定是否使用内存对齐,或是内存对齐到几个字节,以上面的结构体为例:
1)到4字节,同样可指定对齐到8字节。
struct student
{
char name[7];
uint32_t id;
char subject[5];
} __attribute__((aligned(4)));
2)不对齐,结构体的长度,就是各个变量长度的和
struct student
{
char name[7];
uint32_t id;
char subject[5];
} __attribute__ ((packed));
__attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操作系统没关系,跟编译器有关,gcc编译器不是紧凑模式的,在windows下,用vc的编译器也不是紧凑的,用tc的编译器就是紧凑的。例如:
在TC下:struct my{ char ch; int a;} sizeof(int)=2;sizeof(my)=3;(紧凑模式)
在GCC下:struct my{ char ch; int a;} sizeof(int)=4;sizeof(my)=8;(非紧凑模式)
在GCC下:struct my{ char ch; int a;}__attrubte__ ((packed)) sizeof(int)=4;sizeof(my)=5
其他
多个attribute属性
可以在同一个函数声明里使用多个__attribute__,并且实际应用中这种情况是十分常见的。使用方式上,你可以选择两个单独的__attribute__,或者把它们写在一起,例如:
extern void die(const char *format, ...)
__attribute__((noreturn))
__attribute__((format(printf, 1, 2)));
或者
extern void die(const char *format, ...)
__attribute__((noreturn, format(printf, 1, 2)));
和非GNU编译器的兼容性
/* 如果使用的是非GNU C, 那么就忽略__attribute__ */
ifndef __GNUC__
#define __attribute__(x) /*NOTHING*/
#endif
__attribute__的两种写法
1、先声明再定义
void func() __attribute__(...);
void func()
{
...
}
2、直接定义
void __attribute__(...) func()
{
...
}
或
__attribute__(...) void func()
{
...
}