MSVC++ 对象内存模型深入解析与具体应用
前言:本文之所以强调MSVC, 旨在提醒读者在不同平台和解释器下内存布局和实现上存在差异,但编程思想通用,文中内容大多来自笔者实际工作经验和网上搜集,力求正确,但水平有限,如有不当之处,敬请指出
面向对象:本文面向有一定C/C++基础,并且可以读懂部分汇编的读者
版权:欢迎转载,但请注明出处http://www.cppblog.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为字节对齐
数,其取值为1、2、4、8、16,默认是8,取消修改用#pragma pack(),如果结构体某成员的sizeof大于你设置的,则按你的设置来对齐。
本章结束