在 http://www.groad.net/bbs/read.php?tid=1032 中转载了一篇关于 __attribute__ 机制说明的文章,这里再用完整的一般测试代码进行测试,作为加深理解与补充。
1、__attribute__ format 属性
语法格式:
format (archetype, string-index, first-to-check)
参数说明:
archtype : 指定是哪种风格 ();
string-index : 指定传入的第几个参数是格式化字符串;
first-to-check : 指定从函数的第几个参数开始检查上述规则。
具体格式:
__attribute__((format(printf,m,n)));
__attribute__((format(scanf,m,n)));
测试代码:
运行及输出:
说明:
self_printf() 中,第 1 个参数是 const char *format,即格式化字符串;第 2 个参数是省略号 "...",这也就是参数个数不定的参数列表。
在 __attribute__ ((format(printf,1,2))); 里,1 就是表示 const char *format 格式化字符串; 2 表示从第二个参数开始检查,第二个参数即参数列表。
在主函数里调用调用 self_printf() 时,如果传入的参数不符合 printf() 标准函数的格式检查,那么就会发出警告或报错。比如将 self_printf("%d %d %s\n",a, b,buf); 改为self_printf("%d %d\n",a, b,buf); 编译时会提示:
其实,在这里使用了 __attribute__ 属性后,事先就会对调用 seft_printf() 函数做检查。若检查通过,在其后的 self_printf() 内部的 switch() 里再检查格式字符串时,也没有必要再检查 % 了,也就是说,__attribute__ 保证了传入的参数是一定正确的。
另外,__atribute__ format 中的 format 格式字符串,还可以按 scanf, strftime或strfmon 这些函数中的格式字符串规则进行检查。
关于 va_start() , va_arg() , va_end() 的用法见: http://www.groad.net/bbs/read.php?tid=947
2、__attribute_((noreturn)) 属性
noreturn 属性用于 noreturn 函数,它告诉编译器被这个标识了这个属性的函数永不会返回。这可以让编译器生成稍微优化的代码,最重要的是可以消除不必要的警告信息,比如未初始化的变量。C 库函数中的 abort() 和 exit() 的声明格式就采用了这种格式,如:
测试代码:
编译一下:
编译器发出警告,提示已经到达 ‘非void’ 函数的末尾。这里的 '非 void' 函数是 int foo(int n)。因为 foo() 函数应该有一个返回值,但是当 n > 0 时,编译器却无法找到返回值。如果在 printf() 函数后面添加一行,比如 return (1); 那么编译时警告消失。尽管 exitnow() 调用的是 exit() 函数且实际上程序在 n > 0 时也不会到达 printf() 函数,但编译器不会去检查 exitnow() 函数是什么,它仍然认为 exitnow() 后,程序会继续往下走。然而,我们可以在 exitnow() 的前面添加 __attribute__((noreturn))后(上面程序中去掉 foo() 前面的屏蔽部分),那么在不用添加 return (1); 语句的情况下,编译器也不会发出警告,因为 noreturn 属性明确告诉编译器:“ 到我这里,我不会返回了,你无需再发出警告”。
3、__attribute__ const 属性
该属性只能用于带有数值类型参数的函数上。当重复调用带有数值参数的函数时,由于返回值是相同的,所以此时编译器可以进行优化处理,除了第一次需要运算外,其它只需要返回第一次的结果即可,从而提高了效率。该属性主要适用于没有静态状态 (static state) 和副作用的一些函数,并且返回值仅仅依赖输入的参数。
测试代码:
运行及输出:
说明:
如果 square() 函数前不加 const 属性,那么在主函数的 for 循环里,计算机会老老实实的进行 100 次的函数调用。而在加了 const 属性后,square() 函数只调用一次,其余的 99 次都一律直接返回第一次的值 : 25 ,而无需经过再次计算,这是因为 square(5) 的值是固定的。
注意,带有该属性的函数不能有任何副作用或者是静态的状态。所以,像类似 getchar() 或 time() 这样充满“变数”的函数是不适合用该属性的。但是如果加了会怎么样呢?答案是没起作用。看下面代码:
两次运行输出:
由此可见,在 time() 这样的函数上即使加了 const 标签,那也不会看到一直返回一个固定值的情况;如果返回固定时间值,那上面的结果就会有奇数出现。
4、__attribute__ ((packed));
__attribute__((packed)) 属性用于变量和类型,用于变量或结构域时,表示使用最小可能的对齐,用于枚举、结构或联合类型时表示该类型使用最小的内存。如对于结构体,就是它告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐。( 关于结构优化对齐<字节填充>,见:http://www.groad.net/bbs/read.php?tid=1037 )
测试代码:
编译、运行及输出:
如编译所提示的,__attribute__((packed)) 放在结构名 demo_temp 后面是要被忽略的。使用了 __attribute__((packed)) 标识的结构体,输出大小为自身实际字节占据的大小;没有标识的,则输出经过字节填充优化后的大小。
1、__attribute__ format 属性
语法格式:
format (archetype, string-index, first-to-check)
参数说明:
archtype : 指定是哪种风格 ();
string-index : 指定传入的第几个参数是格式化字符串;
first-to-check : 指定从函数的第几个参数开始检查上述规则。
具体格式:
__attribute__((format(printf,m,n)));
__attribute__((format(scanf,m,n)));
测试代码:
引用#include <stdio.h>
#include <stdarg.h>
void self_printf(const char *format,...)__attribute__ ((format(printf,1,2)));
int main()
{
int a = 10;
int b = 8;
int c;
char buf [20]="hello world";
c =10;
self_printf("%d %d %s\n",a, b,buf);
return 0;
}
void self_printf(const char *format,...)
{
int i;
char c, *p;
va_list ap;
va_start (ap,format);
while (*format)
switch (*format++){
case 's':
p = va_arg (ap,char*);
printf ("%s", p);
break;
case 'd':
i = va_arg (ap,int);
printf ("%d ",i);
break;
case 'c':
c = (char)va_arg (ap,int);
printf ("%c ",c);
break;
case '\n':
c = (char)va_arg (ap,int);
printf("\n");
break;
}
va_end (ap);
}
运行及输出:
引用beyes@linux-beyes:~/C/GNU_C_EXT> ./attribute.exe
10 8 hello world
说明:
self_printf() 中,第 1 个参数是 const char *format,即格式化字符串;第 2 个参数是省略号 "...",这也就是参数个数不定的参数列表。
在 __attribute__ ((format(printf,1,2))); 里,1 就是表示 const char *format 格式化字符串; 2 表示从第二个参数开始检查,第二个参数即参数列表。
在主函数里调用调用 self_printf() 时,如果传入的参数不符合 printf() 标准函数的格式检查,那么就会发出警告或报错。比如将 self_printf("%d %d %s\n",a, b,buf); 改为self_printf("%d %d\n",a, b,buf); 编译时会提示:
引用beyes@linux-beyes:~/C/GNU_C_EXT> gcc -Wall -g attribute.c -o attribute.exe
attribute.c: In function ‘main’:
attribute.c:14: warning: too many arguments for format
其实,在这里使用了 __attribute__ 属性后,事先就会对调用 seft_printf() 函数做检查。若检查通过,在其后的 self_printf() 内部的 switch() 里再检查格式字符串时,也没有必要再检查 % 了,也就是说,__attribute__ 保证了传入的参数是一定正确的。
另外,__atribute__ format 中的 format 格式字符串,还可以按 scanf, strftime或strfmon 这些函数中的格式字符串规则进行检查。
关于 va_start() , va_arg() , va_end() 的用法见: http://www.groad.net/bbs/read.php?tid=947
2、__attribute_((noreturn)) 属性
noreturn 属性用于 noreturn 函数,它告诉编译器被这个标识了这个属性的函数永不会返回。这可以让编译器生成稍微优化的代码,最重要的是可以消除不必要的警告信息,比如未初始化的变量。C 库函数中的 abort() 和 exit() 的声明格式就采用了这种格式,如:
引用externvoid exit(int)__attribute__((noreturn));
extern voidabort(void)__attribute__((noreturn));
测试代码:
引用#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/*__attribute__((noreturn))*/ void exitnow ()
{
exit(1);
}
int foo (int n)
{
if (n > 0) {
exitnow();
printf("hello world\n");
} else return0;
}
int main ()
{
int n = 10;
foo (n);
return 0;
}
编译一下:
引用beyes@linux-beyes:~/C/GNU_C_EXT> gcc -g -Wall attr_noreturn.c -o attr_noreturn.exe
attr_noreturn.c: In function ‘foo’:
attr_noreturn.c:17: warning: control reaches end of non-void function
编译器发出警告,提示已经到达 ‘非void’ 函数的末尾。这里的 '非 void' 函数是 int foo(int n)。因为 foo() 函数应该有一个返回值,但是当 n > 0 时,编译器却无法找到返回值。如果在 printf() 函数后面添加一行,比如 return (1); 那么编译时警告消失。尽管 exitnow() 调用的是 exit() 函数且实际上程序在 n > 0 时也不会到达 printf() 函数,但编译器不会去检查 exitnow() 函数是什么,它仍然认为 exitnow() 后,程序会继续往下走。然而,我们可以在 exitnow() 的前面添加 __attribute__((noreturn))后(上面程序中去掉 foo() 前面的屏蔽部分),那么在不用添加 return (1); 语句的情况下,编译器也不会发出警告,因为 noreturn 属性明确告诉编译器:“ 到我这里,我不会返回了,你无需再发出警告”。
3、__attribute__ const 属性
该属性只能用于带有数值类型参数的函数上。当重复调用带有数值参数的函数时,由于返回值是相同的,所以此时编译器可以进行优化处理,除了第一次需要运算外,其它只需要返回第一次的结果即可,从而提高了效率。该属性主要适用于没有静态状态 (static state) 和副作用的一些函数,并且返回值仅仅依赖输入的参数。
测试代码:
引用#include <stdio.h>
__attribute__((const))intsquare(int n)
{
return (n * n);
}
int main()
{
int i;
int total = 0;
for (i=0; i < 100;i++)
total +=square(5)+ i;
printf ("total = %d\n",total);
return 0;
}
运行及输出:
引用beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
total = 7450
说明:
如果 square() 函数前不加 const 属性,那么在主函数的 for 循环里,计算机会老老实实的进行 100 次的函数调用。而在加了 const 属性后,square() 函数只调用一次,其余的 99 次都一律直接返回第一次的值 : 25 ,而无需经过再次计算,这是因为 square(5) 的值是固定的。
注意,带有该属性的函数不能有任何副作用或者是静态的状态。所以,像类似 getchar() 或 time() 这样充满“变数”的函数是不适合用该属性的。但是如果加了会怎么样呢?答案是没起作用。看下面代码:
引用#include <stdio.h>
#include <time.h>
__attribute__((const))time_ttime_test(time_t*t)
{
return (time (t));
}
int main()
{
int i;
time_t t = 0;
for (i=0; i < 5;i++){
printf ("%d\n", (time_test(&t)+i));
sleep(1);
}
return 0;
}
两次运行输出:
引用beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
1250417600
1250417602
1250417604
1250417606
1250417608
beyes@linux-beyes:~/C/GNU_C_EXT> ./attr_const.exe
1250417612
1250417614
1250417616
1250417618
1250417620
由此可见,在 time() 这样的函数上即使加了 const 标签,那也不会看到一直返回一个固定值的情况;如果返回固定时间值,那上面的结果就会有奇数出现。
4、__attribute__ ((packed));
__attribute__((packed)) 属性用于变量和类型,用于变量或结构域时,表示使用最小可能的对齐,用于枚举、结构或联合类型时表示该类型使用最小的内存。如对于结构体,就是它告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐。( 关于结构优化对齐<字节填充>,见:http://www.groad.net/bbs/read.php?tid=1037 )
测试代码:
引用#include <stdio.h>
struct demo {
char i;
char j;
int k;
int l;
double m;
}__attribute__((packed));
typedef structdemo1{
char i;
char j;
int k;
int l;
double m;
}__attribute__((packed))test;
typedef structdemo2{
char i;
char j;
int k;
int l;
double m;
} demo_nopacked;
typedef structdemo3{
char i;
char j;
int k;
int l;
double m;
} demo_temp__attribute((packed));
int main()
{
printf("sizeof demo is : %d\n",sizeof(structdemo));
printf("sizeof demo is : %d\n",sizeof(test));
printf("sizeof demo is : %d\n",sizeof(demo_nopacked));
printf("sizeof demo is : %d\n",sizeof(demo_temp));
return 0;
}
编译、运行及输出:
引用beyes@linux-beyes:~/C/base> gcc -g attr_pack.c -o attr_pack.exe
attr_pack.c:34: warning: ‘packed’ attribute ignored
beyes@linux-beyes:~/C/base> ./attr_pack.exe
sizeof demo is : 18
sizeof demo is : 18
sizeof demo is : 20
sizeof demo is : 20
如编译所提示的,__attribute__((packed)) 放在结构名 demo_temp 后面是要被忽略的。使用了 __attribute__((packed)) 标识的结构体,输出大小为自身实际字节占据的大小;没有标识的,则输出经过字节填充优化后的大小。
__attribute__ 中的 section 属性对代码段起作用,其格式为:
其意是将作用的函数或数据放入指定名为 "section_name" 输入段中。 输入段和输出段是相对于要生成最终的 elf 或 binary 时的 link 过程来说的。link 过程的输入大都是由源代码编译生成的目标文件.o ,那么这些 .o 文件中包含的段相对 link 过程来说就是输入段,而 link 的输出一般是可执行文件 elf 或库等,这些输出文件中也包含段,这些输出文件中的段叫做输出段。输入段和输出段没有必然联系,为互相独立,只是在 link 过程中,link 程序会根据一定的规则 (这些规则来源于 link script),将不同的输入段组合到不同的输出段中。 测试代码-1:
编译:
原来 section 属性不能用来声明局部变量。下面把 var 改为全局变量:
编译通过。下面查看一下 test.o 文件中的 section 信息:
上面,.xxdata 是自定义 section。像在 linux 驱动程序设计中,模块加载函数前有一个 __init 宏,也用了 attribute 的 section 属性,如:
说明:在 linux 内核中,所有标识为 __init 的函数在链接的时候都放在 .init.text 这个区段内。此外,所有的 __init 函数在区段 .initcall.init 中还保存了一份函数指针,在初始化时内核会通过这些函数指针调用这些 __init 函数,并在初始化完成后释放 init 区段 (包括 .init.text, .initcall.iinit 等)。 不但是变量,函数也可以用 section 属性来声明:
编译后,同样用 objdump 查看一下 section 信息:
|