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.

 

 

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