• <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>

            統計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 164783
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            MSVC++ 對象內存模型深入解析與具體應用(一)

            MSVC++ 對象內存模型深入解析與具體應用

            前言:本文之所以強調MSVC, 旨在提醒讀者在不同平臺和解釋器下內存布局和實現上存在差異,但編程思想通用,文中內容大多來自筆者實際工作經驗和網上搜集,力求正確,但水平有限,如有不當之處,敬請指出

            面向對象:本文面向有一定C/C++基礎,并且可以讀懂部分匯編的讀者

            版權:歡迎轉載,但請注明出處http://www.shnenglu.com/dawnbreak/ 保留對本文的一切權力

            目錄
            1. C++基本類型與結構體內存布局
                            Key words: class,  struct, memory alignment

            2.虛表, 多態與動態綁定

                            Key words: Virtual Table, polymiorphism

            3.對象池

                            Key words: object pool , reload, new ,delete

            4.內存泄漏檢測

                            Key words: memory leak detect

            5.智能指針

                            Key words: smart pointer

            6.   編譯期類型約束
               
                            Key words: compile-time ,type-constraint


             Appendix 1: C++堆棧祥解




            第一篇:
            C++基本類型與結構體內存布局

             Reference: http://cnblogs.com/itech
            Key words: class,  struct, memory alignment

            1. 基本類型(basic type

              
             1//test the size of class and struct
             2void TestBasicSizeOf() 
             3
             4    cout << __FUNCTION__ << endl; 
             5    cout << "  sizeof(char)= " << sizeof ( char ) << endl; 
             6    cout << "  sizeof(int)= " << sizeof ( int ) << endl; 
             7    cout << "  sizeof(float)= " << sizeof ( float ) << endl; 
             8    cout << "  sizeof(double)= " << sizeof ( double ) << endl; 
             9
            10    cout << "  sizeof('$')=" << sizeof ( '$' ) << endl; 
            11    cout << "  sizeof(1)= " << sizeof ( 1 ) << endl; 
            12    cout << "  sizeof(1.5f)= " << sizeof ( 1.5f ) << endl; 
            13    cout << "  sizeof(1.5)= " << sizeof ( 1.5 ) << endl; 
            14
            15    cout << "  sizeof(Good!)= " << sizeof ( "Good!" ) << endl ; 
            16
            17    char  str[] = "CharArray!"
            18    int  a[10];  
            19    double  xy[10]; 
            20    cout << "  char str[] = \"CharArray!\"," << " sizeof(str)= " << sizeof (str) << endl; 
            21    cout << "  int a[10]," << " sizeof(a)= " << sizeof (a) << endl; 
            22    cout << "  double xy[10]," << " sizeof(xy)= " <<   sizeof (xy) << endl; 
            23
            24    cout << "  sizeof(void*)= " << sizeof(void*<< endl; 
            25}
            輸出結果:

            2. 結構體與類

            這里的代碼是結構體,但是結構體和類在C++中是通用的,唯一的區別就是默認的訪問方式,struct-public, class-private

             1struct st1
             2{
             3    short number;
             4    float math_grade;
             5    float Chinese_grade;
             6    float sum_grade;
             7    char  level;
             8}
            //20
             9
            10struct st2
            11{
            12    char  level;
            13    short number;
            14    float math_grade;
            15    float Chinese_grade;
            16    float sum_grade;
            17}
            ;//16
            18
            19#pragma pack(1)
            20struct st3
            21{
            22    char  level;
            23    short number;
            24    float math_grade;
            25    float Chinese_grade;
            26    float sum_grade;
            27}
            //15
            28#pragma pack() 
            29
            30void TestStructSizeOf()
            31{
            32    cout << __FUNCTION__ << endl;
            33
            34    cout << "  sizeof(st1)= " << sizeof (st1) << endl;
            35    cout << "  offsetof(st1,number) " << offsetof(st1,number) << endl;
            36    cout << "  offsetof(st1,math_grade) " << offsetof(st1,math_grade) << endl;
            37    cout << "  offsetof(st1,Chinese_grade) " << offsetof(st1,Chinese_grade) << endl;
            38    cout << "  offsetof(st1,sum_grade) " << offsetof(st1,sum_grade) << endl;
            39    cout << "  offsetof(st1,level) " << offsetof(st1,level) << endl;
            40
            41    cout << "  sizeof(st2)= " << sizeof (st2) << endl;
            42    cout << "  offsetof(st2,level) " << offsetof(st2,level) << endl;
            43    cout << "  offsetof(st2,number) " << offsetof(st2,number) << endl;
            44    cout << "  offsetof(st2,math_grade) " << offsetof(st2,math_grade) << endl;
            45    cout << "  offsetof(st2,Chinese_grade) " << offsetof(st2,Chinese_grade) << endl;
            46    cout << "  offsetof(st2,sum_grade) " << offsetof(st2,sum_grade) << endl;
            47
            48
            49    cout << "  sizeof(st3)= " << sizeof (st3) << endl;
            50    cout << "  offsetof(st3,level) " << offsetof(st3,level) << endl;
            51    cout << "  offsetof(st3,number) " << offsetof(st3,number) << endl;
            52    cout << "  offsetof(st3,math_grade) " << offsetof(st3,math_grade) << endl;
            53    cout << "  offsetof(st3,Chinese_grade) " << offsetof(st3,Chinese_grade) << endl;
            54    cout << "  offsetof(st3,sum_grade) " << offsetof(st3,sum_grade) << endl;
            55}

            輸出結果:

            3.內存對齊

            仔細查看上面的輸出結果,會發現同樣的結構體定義僅僅是成員順序不同, 就會造成結構體大小的變化,這就是內存對齊的結果,在計算機的底層進行內存的讀寫的時候,如果內存對齊的話可以提高讀寫效率,下面是VC的默認的內存對齊規則:

            1) 結構體變量的首地址能夠被其最寬基本類型成員的大小所整除;
            2)
            結構體每個成員相對于結構體首地址的偏移量(offset)都是成員大小的整數倍, 如有需要編譯器會在成員之間加上填充字節(internal adding);
            3)
            結構體的總大小為結構體最寬基本類型成員大小的整數倍,如有需要編譯器會在最末一個成員之后加上填充字節(trailing padding)。

            當然VC提供了工程選項/Zp [1|2|4|8|16]可以修改對齊方式,當然我們也可以在代碼中對部分類型實行特殊的內存對齊方式,修改方式為#pragma pack( n )n為字節對齊
            數,其取值為124816,默認是8,取消修改用#pragma pack(),如果結構體某成員的sizeof大于你設置的,則按你的設置來對齊。

            本章結束

             

             

            posted on 2009-03-10 12:57 pear_li 閱讀(2805) 評論(8)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-11 09:01 megax

            還有下文沒?
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-11 09:39 夢在天涯

            1. C++基本類型與結構體內存布局
            Key words: class, struct, memory alignment

            2.虛表, 多態與動態綁定

            Key words: Virtual Table, polymiorphism

            我覺的我總結的還是不錯的哦,你可以直接轉載好了,轉載請注明出處!其實我還有準備下一系列,但是實在是最近太忙了,所以暫停了啊!
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-11 09:40 夢在天涯

            你可以到我的cnblogs.com/itech上加我的msn!
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-14 10:04 gifty

            內存對齊默認是4吧!
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-16 17:48 pear_li

            @gifty
            對于32位系統是4位,對于64位系統是8位
            仁兄看得真仔細
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-03-26 21:18 可微函數

            采用自定義的對齊方式 是不是會對訪問效率產生影響?
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2009-04-02 22:17 pear_li

            @可微函數
            對,會產生很大的影響,因為按照X86尋址方式,如果數據沒有對其,默認方式會先將數據對其再進行操作,而對于IA64的機器情況更糟,系統會終止執行你的程序
              回復  更多評論    

            # re: MSVC++ 對象內存模型深入解析與具體應用 2010-04-03 19:10 sasa官網

            分析的到位 :)
              回復  更多評論    
            欧美国产成人久久精品| 成人a毛片久久免费播放| 久久亚洲中文字幕精品一区| 久久99精品久久久久久9蜜桃| 99久久人人爽亚洲精品美女 | 午夜精品久久久久成人| 久久精品一区二区三区AV| 日韩精品久久无码人妻中文字幕 | 精品久久久无码中文字幕天天| 久久亚洲精品无码播放| 三上悠亚久久精品| 少妇被又大又粗又爽毛片久久黑人| 亚洲αv久久久噜噜噜噜噜| 成人亚洲欧美久久久久| 久久午夜无码鲁丝片| 日批日出水久久亚洲精品tv| 国产精品久久国产精麻豆99网站| 午夜视频久久久久一区| 成人精品一区二区久久久| 国产午夜福利精品久久2021| 久久香综合精品久久伊人| 久久久久久久久久免免费精品 | 久久精品国产一区二区三区 | 国产A三级久久精品| 草草久久久无码国产专区| 狠狠色婷婷综合天天久久丁香| 一本色道久久88—综合亚洲精品| 久久久久亚洲AV成人网人人网站| 国产欧美久久一区二区| 久久久久AV综合网成人| 亚洲人成精品久久久久| 中文字幕无码精品亚洲资源网久久 | 久久久无码精品亚洲日韩软件| 99久久久国产精品免费无卡顿| 久久经典免费视频| 欧美精品国产综合久久| 国内精品九九久久精品| 亚洲国产精品无码久久久秋霞2| 欧美日韩精品久久久免费观看 | av午夜福利一片免费看久久| 亚洲∧v久久久无码精品|