• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            offsetof宏解析


            今天看代碼時,發現一個有用的東東,offsetof(s,m),這是一個宏,MSDN文檔的說明如下:

            Retrieves the offset of a member from the beginning of its parent structure.

            size_t offsetof(
            structName,
            memberName
            );


            Parameters

            structName
            Name of the parent data structure.


            memberName
            Name of the member in the parent data structure for which to determine the offset.


            Return Value


            offsetof returns the offset in bytes of the specified member from the beginning of its parent data structure. It is undefined for bit fields.

            Remarks

            The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.

            Note


            offsetof is not a function and cannot be described using a C prototype.

            跟蹤代碼發現定義如下:

            #define offsetof(s,m) (size_t)&(((s *)0)->m)

            然后到網上查了一下,發現還真的是很有用,附帶一位大俠的解說:

            struct AAA
            {
                int i;
                int j;
            };

            struct AAA *pAAA;
            pAAA=new AAA;


            這時,pAAA實際上是一個Pointer, 指向某一確定的內存地址,比如0x1234;
            而 pAAA->i 整體是一個int型變量,其地址是&(pAAA->i) ,'&'為取址運算符;
            那么&(pAAA->i)一定等于0x1234,因為i是結構體AAA的第一個元素。
            而&(pAAA->j)一定是0x1234 + 0x4 = 0x1238; 因為sizeof(int) = 4;

            這個做法的巧妙之處就是:它把“0”作為上例中的pAAA,那么 &(pAAA->j)就是j的offset啦。

            解析結果是:
            (s *)0 ,將 0 強制轉換為Pointer to "s"
            可以記 pS = (s *)0 ,pS是指向s的指針,它的值是0;
            那么pS->m就是m這個元素了,而&(pS->m)就是m的地址,而在本例中就是offset啦

            再把結果強制轉換為size_t型的就OK 了,size_t其實也就是int啦!!

            也就是說:

            0 ---> (s *)0

            原來的0是數值類型,現在是結構體指針類型,盡管類型變了,但其值還是不變,也就是說還是0,但這個值的意義變了,現在是地址,而不是數值。

            &(((s *)0)->m)求出字段m的地址值,但由于首地址是0,所以&(((s *)0)->m)求出字段m相對于首地址的偏移值。

            posted on 2007-09-24 19:04 lovedday 閱讀(3761) 評論(0)  編輯 收藏 引用 所屬分類: ▲ C Program

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            亚洲精品97久久中文字幕无码| 久久精品国产欧美日韩| yy6080久久| 久久大香香蕉国产| 久久久精品午夜免费不卡| 香蕉久久影院| 精品久久久久久久无码| 手机看片久久高清国产日韩| 伊人久久精品影院| 成人亚洲欧美久久久久| 久久久久久极精品久久久| 精品久久久无码21p发布| 99久久精品国产一区二区| 97精品伊人久久大香线蕉| 99久久婷婷国产综合精品草原| 久久天天婷婷五月俺也去| 国产精品久久一区二区三区| 伊人久久一区二区三区无码| 精品国产青草久久久久福利| 日产精品99久久久久久| 伊人久久大香线蕉综合热线| 91精品无码久久久久久五月天| 一本色道久久综合狠狠躁| 久久精品国产第一区二区| 国内精品久久久久久99| 97精品伊人久久大香线蕉| 欧美与黑人午夜性猛交久久久| 国产精品久久久天天影视香蕉| 久久99热国产这有精品| 中文字幕无码免费久久| 久久热这里只有精品在线观看| 久久久久久极精品久久久| 久久97久久97精品免视看| 嫩草影院久久99| 久久91亚洲人成电影网站| 丁香狠狠色婷婷久久综合| 久久精品国产91久久综合麻豆自制| 97精品久久天干天天天按摩| 色婷婷综合久久久久中文 | 国产成人精品久久一区二区三区av| 久久人人爽人人爽人人片AV不|