首頁>技術>

@[TOC]

結構體在記憶體中是如何儲存的
int main(){    Student stu;    stu.id = 123456;    strcpy(stu.name,"feizhufeifei");    stu.math = 90;    stu.PE = 80;    printf("Student:%p\r\n",&stu);    printf("stu.ID:%p\r\n",&stu.ID);    printf("stu.name:%p\r\n",&stu.name);    printf("stu.math:%p\r\n",&stu.math);    return 0;}

  列印結果如下:

不太理解的再看下這兩個例子struct A { int a; char b; int c; char d; };a 偏移為 0 , b 偏移為 4 , c 偏移為 8 (大於 4 + 1 的 4 的最小整數倍), d 偏移為 12 。 A 對齊為 4 ,大小為 16 。struct B { int a; char b; char c; long d; };a 偏移為 0 , b 偏移為 4 , c 偏移為 5 , d 偏移為 8 。 B 對齊為 8 , 大小為 16 。

  我們可以看到,結構體中成員變數在記憶體中儲存的其實是偏移地址。也就是說結構體A的地址+成員變數的偏移地址 = 結構體成員變數的起始地址。因此,我們也可以根據結構體變數的起始地址和成員變數的偏移地址來反推出結構體A的地址。

container_of宏
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)#define container_of(ptr, type, member) ({          \        const typeof(((type *)0)->member)*__mptr = (ptr);    \    (type *)((char *)__mptr - offsetof(type, member)); })

  首先看下三個引數, ptr是成員變數的指標, type是指結構體的型別, member是成員變數的名字。

  container_of宏的作用是透過結構體內某個成員變數的地址和該變數名,以及結構體型別。找到該結構體變數的地址。這裡使用的是一個利用編譯器技術的小技巧,即先求得結構成員在結構中的偏移量,然後根據成員變數的地址反過來得出主結構變數的地址。下面具體分析下各個部分:

typeof

  首先看下typeof,是用於返回一個變數的型別,這是GCC編譯器的一個擴充套件功能,也就是說typeof是編譯器相關的。既不是C語言規範的所要求,也不是某個標準的一部分。

int main(){    int a = 5;    //這裡定義一個和a型別相同的變數b    typeof(a) b  = 6;    printf("%d,%d\r\n",a,b);//5 6    return 0;}
(((type *)0)->member)

  ((TYPE *)0)將0轉換為type型別的結構體指標,換句話說就是讓編譯器認為這個結構體是開始於程式段起始位置0,開始於0地址的話,我們得到的成員變數的地址就直接等於成員變數的偏移地址了。   (((type *)0)->member) 引用結構體中MEMBER成員。

typedef struct student{    int id;    char name[30];    int math;}Student;int main(){    //這裡時把結構體強制轉換成0地址,然後列印name的地址。    printf("%d\r\n",&((Student *)0)->name);//4    return 0;}
const typeof(((type )0)->member)__mptr = (ptr);

   這句程式碼意思是用typeof()獲取結構體裡member成員屬性的型別,然後定義一個該型別的臨時指標變數__mptr,並將ptr所指向的member的地址賦給__mptr;   為什麼不直接使用 ptr 而要多此一舉呢? 我想可能是為了避免對 ptr 及prt 指向的內容造成破壞。

offsetof(type, member))
((size_t) &((TYPE*)0)->MEMBER)

   size_t是標準C庫中定義的,在32位架構中被普遍定義為:

typedef unsigned int size_t;

  而在64位架構中被定義為:

typedef unsigned long size_t;

  可以從定義中看到,size_t是一個非負數,所以size_t通常用來計數(因為計數不需要負數區):

for(size_t i=0;i<300;i++)

  為了使程式有很好的移植性,因此核心使用size_t和,而不是int,unsigned。((size_t) &((TYPE*)0)->MEMBER) 結合之前的解釋,我們可以知道這句話的意思就是求出MEMBER相對於0地址的一個偏移值。

(type *)((char *)__mptr - offsetof(type, member))

   這句話的意思就是,把 __mptr 轉換成 char * 型別, 因為 offsetof 得到的偏移量是以位元組為單位。 兩者相減得到結構體的起始位置, 再強制轉換成 type 型別。

舉例
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)#define container_of(ptr, type, member) ({ \        const typeof( ((type *)0)->member ) *__mptr = (ptr); \        (type *)( (char *)__mptr - offsetof(type,member) );})typedef struct student{    int id;    char name[30];    int math;}Student;int main(){          Student stu;        Student *sptr = NULL;        stu.id = 123456;        strcpy(stu.name,"feizhufeifei");        stu.math = 90;        sptr = container_of(&stu.id,Student,id);        printf("sptr=%p\n",sptr);        sptr = container_of(&stu.name,Student,name);        printf("sptr=%p\n",sptr);        sptr = container_of(&stu.math,Student,id);        printf("sptr=%p\n",sptr);        return 0;    }

  執行結果如下:

sptr=0xffffcb90sptr=0xffffcb90sptr=0xffffcbb4

  宏展開可能會看的更清楚一些

int main(){          Student stu;        Student *sptr = NULL;        stu.id = 123456;        strcpy(stu.name,"feizhufeifei");        stu.math = 90;        //展開替換        sptr = ({ const unsigned char  *__mptr = (&stu.id); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->id) );});        printf("sptr=%p\n",sptr);        //展開替換        sptr = ({ const unsigned char  *__mptr = (&stu.name); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->name) );});        printf("sptr=%p\n",sptr);        //展開替換        sptr = ({ const unsigned int *__mptr = (&stu.math); (Student *)( (char *)__mptr - ((size_t) &((Student *)0)->math) );});        printf("sptr=%p\n",sptr);        return 0;    }

  養成習慣,先贊後看!如果覺得寫的不錯,歡迎關注,點贊,收藏,轉發,謝謝!   以上程式碼均為測試後的程式碼。如有錯誤和不妥的地方,歡迎指出。

17
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 拒絕造輪子!如何移植並使用Linux核心的通用連結串列