MSVC++ 對(duì)象內(nèi)存模型深入解析與具體應(yīng)用
前言:本文之所以強(qiáng)調(diào)MSVC, 旨在提醒讀者在不同平臺(tái)和解釋器下內(nèi)存布局和實(shí)現(xiàn)上存在差異,但編程思想通用,文中內(nèi)容大多來自筆者實(shí)際工作經(jīng)驗(yàn)和網(wǎng)上搜集,力求正確,但水平有限,如有不當(dāng)之處,敬請(qǐng)指出
面向?qū)ο螅罕疚拿嫦蛴幸欢?/span>C/C++基礎(chǔ),并且可以讀懂部分匯編的讀者
版權(quán):歡迎轉(zhuǎn)載,但請(qǐng)注明出處http://www.shnenglu.com/dawnbreak/, 保留對(duì)本文的一切權(quán)力
目錄
1. C++基本類型與結(jié)構(gòu)體內(nèi)存布局
Key words: class, struct, memory alignment
2.虛表, 多態(tài)與動(dòng)態(tài)綁定
Key words: Virtual Table, polymiorphism
3.對(duì)象池
Key words: object pool , reload, new ,delete
4.內(nèi)存泄漏檢測(cè)
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
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
}
輸出結(jié)果:

2. 結(jié)構(gòu)體與類
這里的代碼是結(jié)構(gòu)體,但是結(jié)構(gòu)體和類在C++中是通用的,唯一的區(qū)別就是默認(rèn)的訪問方式,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
}
輸出結(jié)果:
3.內(nèi)存對(duì)齊
仔細(xì)查看上面的輸出結(jié)果,會(huì)發(fā)現(xiàn)同樣的結(jié)構(gòu)體定義僅僅是成員順序不同, 就會(huì)造成結(jié)構(gòu)體大小的變化,這就是內(nèi)存對(duì)齊的結(jié)果,在計(jì)算機(jī)的底層進(jìn)行內(nèi)存的讀寫的時(shí)候,如果內(nèi)存對(duì)齊的話可以提高讀寫效率,下面是VC的默認(rèn)的內(nèi)存對(duì)齊規(guī)則:
1) 結(jié)構(gòu)體變量的首地址能夠被其最寬基本類型成員的大小所整除;
2) 結(jié)構(gòu)體每個(gè)成員相對(duì)于結(jié)構(gòu)體首地址的偏移量(offset)都是成員大小的整數(shù)倍, 如有需要編譯器會(huì)在成員之間加上填充字節(jié)(internal adding);
3) 結(jié)構(gòu)體的總大小為結(jié)構(gòu)體最寬基本類型成員大小的整數(shù)倍,如有需要編譯器會(huì)在最末一個(gè)成員之后加上填充字節(jié)(trailing padding)。
當(dāng)然VC提供了工程選項(xiàng)/Zp [1|2|4|8|16]可以修改對(duì)齊方式,當(dāng)然我們也可以在代碼中對(duì)部分類型實(shí)行特殊的內(nèi)存對(duì)齊方式,修改方式為#pragma pack( n ),n為字節(jié)對(duì)齊
數(shù),其取值為1、2、4、8、16,默認(rèn)是8,取消修改用#pragma pack(),如果結(jié)構(gòu)體某成員的sizeof大于你設(shè)置的,則按你的設(shè)置來對(duì)齊。
本章結(jié)束