C语言的属性:__attribute__

来源:互联网 发布:数据结构与算法知识点 编辑:程序博客网 时间:2024/05/03 00:22

1. 变量的属性

详细内容参考:变量的属性
注:
1)_attribute_后面必须为两对括号:((…))
2)属性关键字的前后也可加“_”,如aligned可为__aligned_

1.1 对齐(aligned (alignment))

指定变量或结构域的起始地址对齐(以字节为单位):

int x __attribute__ ((aligned (16))) = 0;   // 16字节对齐struct foo {     int x[2] __attribute__ ((aligned (8))); // 8字节对齐};  // ask the compiler to align a variable or field to // the maximum useful alignment for the target machine you are compiling forshort array[3] __attribute__ ((aligned));

1.2 紧凑型 (packed)

变量或结构域以最小对齐单位对齐,如变量以字节对齐,结构域以位对齐:

      struct foo      {        char a;        int x[2] __attribute__ ((packed)); // it immediately follows a      };

1.3 指定代码段(section (“section-name”))

把变量或函数放于指定的代码段:

      struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };      struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };      char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };      int init_data __attribute__ ((section ("INITDATA"))) = 0;      main()      {        /* Initialize stack pointer */        init_sp (stack + sizeof (stack));        /* Initialize initialized data */        memcpy (&init_data, &data, &edata - &data);        /* Turn on the serial ports */        init_duart (&a);        init_duart (&b);      }

1.4 向量尺寸(vector_size (bytes))

指定向量的大小,以字节为单位:

    int foo __attribute__ ((vector_size (16)));    int foo __attribute__ ((__vector_size__ (16)));    struct S { int a; };    struct S  __attribute__ ((vector_size (16))) foo;

2. 函数的属性

详细内容参考:函数的属性

3. 类型的属性

详细内容参考:类型的属性

typedef int more_aligned_int __attribute__ ((aligned (8)));typedef long HEXAGON_VecPred128 __attribute__((__vector_size__(128)))                                __attribute__((aligned(128)));
0 0
原创粉丝点击