通過結構體某個成員的地址計算結構體首地址 (轉)

來源:互聯網
上載者:User

標籤:

最近在CU論壇上有很多人在問這樣一個問題:給出一個結構體成員的地址計算該結構體的起始地址。其實這個題我之前也沒有接觸過,據說核心代碼中有這樣用的,但還沒有看到。不過覺得這個題的解決方案還是有一定技巧的,就總結一下。下面是實現的代碼。

 1 /* 2 Author: Godbach 3 Date: Oct 23, 2008 4 */ 5 #include <stdio.h> 6 #define STRUCT_OFFSET(stru_name, element) (unsigned long)&((struct stru_name*)0)->element 7 struct stru_addr  8 { 9     int a;10     char b;11     int d;12     char c;13 14 };15 16 int main(void)17 {18     struct stru_addr s;19     printf("start addr of s = %x\n", &s.a);20     21     unsigned long offset = STRUCT_OFFSET(stru_addr, c);22 23     printf("c_addr = %x, offset = %u\n", &s.c, offset);24     printf("start addr of s caculated from c addr: %x\n", (char *)&s.c - offset);25     return 0;26 }

其實整個程式中最關鍵的部分就是如何求出結構體中某個成員相對於結構體首地址的位移量。這裡的解決方案是:假設存在一個虛擬位址0,將該地址強制轉換成為該結構體指標類型(struct stru_name*)0。那麼地址0開始到sizeof(struct)-1長度的記憶體地區就可以視為一個結構體的記憶體。這樣結構體中任何一個元素都可以通過對該結構體指標解引用得到。由於該結構體的起始地址為0, 因此任何一個成員的地址應該等於其相對於結構體起始地址的位移,這也就是計算位移量的方法:(unsigned long)&((struct stru_name*)0)->element。

上面程式執行的結果如下:

1 [[email protected] tmp]# ./a.out 2 start addr of s = bf81b8203 c_addr = bf81b82c, offset = 124 start addr of s caculated from c addr: bf81b820

上述的結果中還同時考慮了結構體內的對齊問題。

原文地址:http://blog.chinaunix.net/uid-10167808-id-25940.html

通過結構體某個成員的地址計算結構體首地址 (轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.