C语言中__attribute__的用法(总3页)

__attribute__机制
GNU C的一大特(却不被初学者所知)就是__attribute__机制。__attribute__可以设置函数属性(Function    Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。__attribute__书写特征是:__attribute__前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__参数。__attribute__语法格式为:__attribute__ ((attribute-list))其位置约束为:放于声明的尾部“;”之前。函数属性(Function Attribute)函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。__attribute__机制也很容易同非GNU应用程序做到兼容之功效。GNU CC需要使用 –Wall编译器来击活该功能,这是控制警告信息的一个很好的方式。下面介绍几个常见的属性参数。
__attribute__ format
该__attribute__属性可以给被声明的函数加上类似printf或者scanf的特征,它可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配。该功能十分有用,尤其是处理一些很难发现的bug。
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);
n:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几,注意,有时函数参数里还有“隐身”的呢,后面会提到;
在使用上,__attribute__((format(printf,m,n)))是常用的,而另一种却很少见到。下面举例说明,其中myprint为自己定义的一个带有可变参数的函数,其功能类似于printf:
//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)));
scanf用法c++
需要特别注意的是,如果myprint是一个函数的成员函数,那么m和n的值可有点“悬乎”了,例如:
//m=3;n=4
extern void myprint(int l,const char *format,...) __attribute__((format(printf,3,4)));
2.3.1 指定变量的属性编译器的关键字 __attribute__ 用来指定变量或结构位域的特殊属性。关键字后的双括弧中的内容是属性说明。
下面是目前支持的变量属性:
• address (addr)• aligned (alignment)• boot • deprecated• fillupper• far• mode (mode)• near• noload• packed• persistent• reverse (alignment)• section ("section-name")• secure • sfr (address)• space (space)• transparent_union• unordered• unused• weakweak也可以通过在关键字前后使用 __(双下划线)来指定属性(例如,用__aligned__代替aligned)。这样将使你在头文件中使用它们时不必考虑会出现与宏同名的情况。要指定多个属性,可在双括弧内用逗号将属性分隔开,例如: __attribute__ ((aligned (16), packed))。