C语言杂记

1.结构体赋值的一种方法

typedef struct Fiber_t
{
    int a;
    int b;
    int c;
} Fiber_t;

int main() 
{
    Fiber_t tmp;
    tmp = (Fiber_t) { .a = 1, .b = 3,.c = 4 };
    printf("%d,%d,%d",tmp.a,tmp.b,tmp.c); 
}
输出:

1,3,4
--------------------------------

2.__attribute__中section的用法

__attribute__这个关键词是GNU编译器中的编译属性,ARM编译器也支持这个用法。__attribute__主要用于改变所声明或定义的函数或 数据的特性

提到section,就得说RO RI ZI了,在ARM编译器编译之后,代码被划分为不同的段,RO Section(ReadOnly)中存放代码段和常量,RW Section(ReadWrite)中存放可读写静态变量和全局变量,ZI Section(ZeroInit)是存放在RW段中初始化为0的变量

__attribute__((section("section_name"))),其作用是将作用的函数或数据放入指定名为"section_name"对应的段中

Example  
/* in RO section */  
const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };  
/* in RW section */  
long long rw[10] __attribute__ ((section ("RW")));  
/* in ZI section *  
long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/  
Example  
In the following example, Function_Attributes_section_0 is placed into the RO section new_section rather than .text.  
  
void Function_Attributes_section_0 (void)  
    __attribute__ ((section ("new_section")));  
void Function_Attributes_section_0 (void)  
{  
    static int aStatic =0;  
    aStatic++;  
}  
In the following example, section function attribute overrides the #pragma arm section setting.  
  
#pragma arm section code="foo"  
  int f2()  
  {  
      return 1;  
  }                                  // into the 'foo' area  
  __attribute__ ((section ("bar"))) int f3()  
  {  
      return 1;  
  }                                  // into the 'bar' area  
  int f4()  
  {  
      return 1;  
  }                                  // into the 'foo' area  
#pragma arm section  

3. __align(n)的使用

__align 关键字指示编译器在 n 字节边界上对齐变量。

n是对齐边界

对于局部变量,n 值可为 1、2、4 或 8。

对于全局变量,n 可以具有最大为 2 的 0x80000000 次幂的任何值。

__align 关键字紧靠变量名称前面放置。

示例:

__align(8) char buffer[128];  // buffer starts on eight-byte boundary
void foo(void)
{
    ...
    __align(16) int i; // this alignment value is not permitted for
                       // a local variable
    ...
}

__align(16) int i; // permitted as a global variable.

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章