Chinaunix首页 | 论坛 | 博客
  • 博客访问: 250440
  • 博文数量: 181
  • 博客积分: 215
  • 博客等级: 民兵
  • 技术积分: 313
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 19:39












2012-05-19 12:47:59

原文地址:linux c __attribute__详解 作者:ubuntuer

1.Declaring Attributes of Functions
  1.1 alias ("target"):函数别名
    The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,


void __f () { /* Do something. */; }
              void f () __attribute__ ((weak, alias ("__f")));

  1.2 constructor和destructor
    The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.        

   1.3 nonnull (arg-index, ...),参数不能为空
    The nonnull attribute specifies that some function parameters should be non-null pointers. For instance, the declaration:
              extern void *
              my_memcpy (void *dest, const void *src, size_t len)
                  __attribute__((nonnull (1, 2)));
   1.4 noreturn没有返回值
    A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,

              void fatal () __attribute__ ((noreturn));
              fatal (/* ... */)
                /* ... */ /* Print error message. */ /* ... */
                exit (1);
   我认为的function attribute就这么多.详见:gcc doc什么都有
[root@mip-123456 attribute]# cat attribute.c

char* const_foo() __attribute__ ((const));
void before_main() __attribute__ ((constructor));
void after_main() __attribute__ ((destructor));

void __foo() { printf("this is foo\n");}
void f() __attribute__ ((weak,alias("__foo")));

char* const_foo()
 char* err = (char*)malloc(10*sizeof(char));
 return err;

void before_main() { printf("before main\n");}
void after_main() { printf("after main\n");}

int main()
 char* str;
 str = const_foo();
 str = NULL;

 return 0;

[root@mip-123456 attribute]# ./attribute
before main
this is foo
this is foo
after main

注意上面的free,没有free的话,会出现mem leak,可以用我以前提到的 valgrind ./attribute

2. Specifying Attributes of Variables
   2.1 aligned (alignment)
    This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

     int x __attribute__ ((aligned (16))) = 0;
     __attribute__ ((aligned(x)))

     __attribute__ ((aligned))时
   Whenever you leave out the alignment factor in an aligned attribute specification, the compiler automatically sets the alignment for the declared variable or field to the largest alignment which is ever used for any data type on the target machine you are compiling for. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way.

The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well. See below.

Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8 byte alignment, then specifying aligned(16) in an __attribute__ will still only provide you with 8 byte alignment. See your linker documentation for further information.

    The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

    Here is a structure in which the field x is packed, so that it immediately follows a:

              struct foo
                char a;
                int x[2] __attribute__ ((packed));

[root@mip-123456 attribute]# cat var_attribute.c

struct foo
  char a;
  int x[4] __attribute__ ((packed));

struct bar8
  char a;
  int x[4] __attribute__ ((aligned(8)));

struct bar16
  char a;
  int x[4] __attribute__ ((aligned(16)));

struct bar32
  char a;
  int x[4] __attribute__ ((aligned(32)));

struct bar
  //char a;
  char x[4] __attribute__ ((aligned));

struct bar_16
  char a;
  int x[4] __attribute__ ((aligned));

int main()
  ,sizeof(struct foo),sizeof(struct bar),sizeof(struct bar_16),sizeof(struct bar8),sizeof(struct bar16),sizeof(struct bar32));
 return 0;

[root@mip-123456 attribute]# ./var_attribute

3. Specifying Attributes of Types
     struct S {
          short f[3];
      } __attribute__ ((aligned (8)));

      struct S {
          short f[3];
      } __attribute__ ((packed));
阅读(671) | 评论(0) | 转发(0) |