__attribute__

GNU C的一大特点就是__attribute__机制。__attribute__能够设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
 
关键字__attribute__ 也能够对结构体(struct )或共用体(union )进行属性设置。大体有六个参数值能够被设定,即:aligned, packed, transparent_union, unused, deprecated 和 may_alias 。
 
一、aligned (alignment)
 
该属性设定一个指定大小的对齐格式(以字节 为单位),例如:
struct S {
  short b[3];
} __attribute__ ((aligned (8)));
typedef int int32_t __attribute__ ((aligned (8)));
该声明将强制编译器确保(尽它所能)变量类 型为struct S 或者int32_t 的变量在分配空间时采用8 字节对齐方式。
 
二、packed
使用该属性对struct 或者union 类型进行定义,不使用内存对齐,packed_struct 类型的变量数组中的值将会牢牢的靠在一块儿,aligned 属性使被设置的对象占用更多的空间,相反的,使用packed 能够减少对象占用的空间。
 
三、unused 
 使用该属性,表示可能用不到,函数参数中使用,表示参数能够不传,编译器也不会报错
 
 
 
__attribute__ 能够对函数属性进行设置,有const,noreturn
 
一、
__attribute__((noreturn)) 表示函数可能没有返回值,编译器处理时,没有返回值也不会报错
 
本站公众号
   欢迎关注本站公众号,获取更多信息