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
2
void 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
1
struct st1
2

{
3
short number;
4
float math_grade;
5
float Chinese_grade;
6
float sum_grade;
7
char level;
8
}; //20
9
10
struct 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)
20
struct 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
30
void 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為字節對齊
數,其取值為1、2、4、8、16,默認是8,取消修改用#pragma pack(),如果結構體某成員的sizeof大于你設置的,則按你的設置來對齊。
本章結束