统计

  • 随笔 - 50
  • 文章 - 42
  • 评论 - 147
  • 引用 - 0

留言簿(6)

随笔分类

文章分类

Link

搜索

  •  

积分与排名

  • 积分 - 160080
  • 排名 - 161

最新评论

阅读排行榜

评论排行榜

MSVC++ 对象内存模型深入解析与具体应用(一)

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为字节对齐
数,其取值为124816,默认是8,取消修改用#pragma pack(),如果结构体某成员的sizeof大于你设置的,则按你的设置来对齐。

本章结束

 

 

posted on 2009-03-10 12:57 pear_li 阅读(2767) 评论(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官网

分析的到位 :)
  回复  更多评论    

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理