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

            統(tǒng)計

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

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 164814
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

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

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

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

            面向?qū)ο螅罕疚拿嫦蛴幸欢?/span>C/C++基礎(chǔ),并且可以讀懂部分匯編的讀者

            版權(quán):歡迎轉(zhuǎn)載,但請注明出處http://m.shnenglu.com/dawnbreak/ 保留對本文的一切權(quán)力

            目錄
            1. C++基本類型與結(jié)構(gòu)體內(nèi)存布局
                            Key words: class,  struct, memory alignment

            2.虛表, 多態(tài)與動態(tài)綁定

                            Key words: Virtual Table, polymiorphism

            3.對象池

                            Key words: object pool , reload, new ,delete

            4.內(nèi)存泄漏檢測

                            Key words: memory leak detect

            5.智能指針

                            Key words: smart pointer

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


             Appendix 1: C++堆棧祥解




            第一篇:
            C++基本類型與結(jié)構(gòu)體內(nèi)存布局

             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}
            輸出結(jié)果:

            2. 結(jié)構(gòu)體與類

            這里的代碼是結(jié)構(gòu)體,但是結(jié)構(gòu)體和類在C++中是通用的,唯一的區(qū)別就是默認的訪問方式,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}

            輸出結(jié)果:

            3.內(nèi)存對齊

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

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

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

            本章結(jié)束

             

             

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

            評論

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

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

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

            1. C++基本類型與結(jié)構(gòu)體內(nèi)存布局
            Key words: class, struct, memory alignment

            2.虛表, 多態(tài)與動態(tài)綁定

            Key words: Virtual Table, polymiorphism

            我覺的我總結(jié)的還是不錯的哦,你可以直接轉(zhuǎn)載好了,轉(zhuǎn)載請注明出處!其實我還有準備下一系列,但是實在是最近太忙了,所以暫停了?。?/div>
              回復  更多評論    

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

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

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

            內(nèi)存對齊默認是4吧!
              回復  更多評論    

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

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

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

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

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

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

            # re: MSVC++ 對象內(nèi)存模型深入解析與具體應用 2010-04-03 19:10 sasa官網(wǎng)

            分析的到位 :)
              回復  更多評論    
            91精品观看91久久久久久| 亚洲女久久久噜噜噜熟女| 999久久久免费精品国产| 99久久99久久| 国产精品va久久久久久久| 久久亚洲欧洲国产综合| 欧美熟妇另类久久久久久不卡 | 亚洲精品乱码久久久久久中文字幕 | 欧美日韩精品久久久免费观看| 伊人 久久 精品| 国产91色综合久久免费| 久久伊人中文无码| 国内精品久久久久久99| 久久这里有精品视频| 国产精品毛片久久久久久久| 久久亚洲AV无码西西人体| 久久综合九色综合97_久久久| 四虎国产精品免费久久| 日韩欧美亚洲综合久久影院d3| 91麻豆国产精品91久久久| 99久久精品国产一区二区蜜芽| 久久婷婷五月综合国产尤物app| 1000部精品久久久久久久久| 欧美精品国产综合久久| 久久久久一级精品亚洲国产成人综合AV区 | 久久精品a亚洲国产v高清不卡| 久久久久亚洲av毛片大| 91精品国产色综久久| 热re99久久精品国99热| 国产偷久久久精品专区| 久久乐国产综合亚洲精品| 精品久久久久久无码中文字幕 | 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 欧美日韩精品久久久久| 四虎国产精品免费久久5151| 久久99国产综合精品女同| 久久亚洲AV成人出白浆无码国产| 久久亚洲精品无码VA大香大香| 久久精品极品盛宴观看| 人妻无码αv中文字幕久久琪琪布| 久久亚洲电影|