剖析 C++中的结构体与sizeof
来源:互联网 发布:最好的网络兼职平台 编辑:程序博客网 时间:2024/04/29 02:31
提问的帖子:
#include<iostream.h>
class Base
{
public:
int x;
int y;
int h[2];
char a;
char b[3];
char c[1];
};
void main()
{
Base dd;
cout << sizeof(dd) << endl;
}
为什么结果输出为24?
帖子地址:http://community.csdn.net/Expert/topic/5478/5478106.xml?temp=.6268732
//----------------------------------------------------------
1) struct MyStruct
{
int dda;
int dda1;
double type ;
short duan ;
int xx;
int ss;
int kk;
};//输出
s 0x0012FF60
&s.dda 0x0012FF60
&s.dda1 0x0012FF64
&s.type 0x0012FF68
&s.duan 0x0012FF70
&s.xx 0x0012FF74
----------------------------------
32
32
0
4
8
16
20
24
28
Press any key to continue
2) struct MyStruct
{
int dda;
double dda1;
int type ;
short duan ;
int xx;
int ss;
int kk;
};
输出:
s 0x0012FF58
&s.dda 0x0012FF58
&s.dda1 0x0012FF60
&s.type 0x0012FF68
&s.duan 0x0012FF6C
&s.xx 0x0012FF70
----------------------------------------
40
40
0
8
16
20
24
28
32
Press any key to continue
s 0x0012FF60
&s.dda 0x0012FF60
&s.dda1 0x0012FF68
&s.type 0x0012FF6C
&s.duan 0x0012FF70
&s.xx 0x0012FF74
----------------------------------------
32
32
0
8
12
16
20
24
28
Press any key to continue
void main()
{
MyStruct s;
cout << "s " << &s << endl;
cout << "&s.dda " << &s.dda << endl;
cout << "&s.dda1 " << &s.dda1 << endl;
cout << "&s.type " << &s.type << endl;
cout << "&s.duan " << &s.duan << endl;
cout << "&s.xx " << &s.xx << endl;
cout<< "----------------------------------------" << endl;
cout<<sizeof(MyStruct)<<endl ;
cout<<sizeof(s)<<endl ;
cout<<offsetof(MyStruct,dda)<<endl ;
cout<<offsetof(MyStruct,dda1)<<endl ;
cout<<offsetof(MyStruct,type)<<endl ;
cout<<offsetof(MyStruct,duan)<<endl ;
cout<<offsetof(MyStruct,xx)<<endl ;
cout<<offsetof(MyStruct,ss)<<endl ;
cout<<offsetof(MyStruct,kk)<<endl ;
}
{
double dda;
int dda1;
int type ;
short duan ;
int xx;
int ss;
int kk;
};
输出:
//----------------------------------------------------------
C++中的结构体与sizeof
可学习参考得其他关于对齐的帖子:
http://www.cppblog.com/cc/archive/2006/08/01/10765.html
http://blog.csdn.net/wqf363/archive/2006/11/26/1415628.aspx
http://blog.csdn.net/yanjun_1982/archive/2005/10/17/507871.aspx
http://dev.csdn.net/article/56/56202.shtm
http://dev.csdn.net/article/48/48195.shtm
http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&rls=com.microsoft:zh-cn:IE-SearchBox&rlz=1I7GGLJ&q=%E5%AD%97%E8%8A%82%E5%AF%B9%E9%BD%90&suggest=0&sa=X&oi=cjkrefinements&ct=result&cd=1
http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&rls=com.microsoft:zh-cn:IE-SearchBox&rlz=1I7GGLJ&q=%E5%86%85%E5%AD%98%E5%AF%B9%E9%BD%90&suggest=1&sa=X&oi=cjkrefinements&ct=result&cd=2
----------------------------------
<script type="text/javascript"><!--google_ad_client = "pub-2019358045871963";google_ad_width = 336;google_ad_height = 280;google_ad_format = "336x280_as";google_ad_type = "text_image";google_ad_channel = "";//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 剖析 C++中的结构体与sizeof
- C++中的结构体与sizeof
- C++中的结构体与sizeof
- C语言的sizeof与结构体的对齐
- C语言:sizeof(结构体)
- 结构体大小与sizeof
- C中的位域与sizeof
- c语言 sizeof()求结构体大小
- 学习笔记-C/C++-结构体与sizeof,内存对齐的题目怎么做
- 学习笔记-C/C++-结构体与sizeof,内存对齐的题目怎么做
- sizeof(char *) = 8——C语言中的数据长度、结构体及内存对齐
- (必看)C结构体与C结构体之位域(位段)的sizeof问题.
- sizeof结构体与内存对齐
- C与C++中的结构体
- C语言中的联合体与结构体
- C语言高频面试题之sizeof与指针和结构体专题总结
- C语言高频面试题之sizeof与指针和结构体专题总结
- C语言中的sizeof
- 跨浏览器的innerHTML.event.srcElment.offsetLeft
- 变
- 魔戒
- Tomcat 配置集锦
- .NET 访问 Oracle 数据库相关
- 剖析 C++中的结构体与sizeof
- JSP中连接SQL 2000数据库的问题总结
- Java 6 : 自定义国际化(I18N)服务
- Testing....
- 在线编辑word文档 可保存到服务器
- 这是我的第一篇博客文章
- 规范化-数据库设计原则
- 识别汉字和全角字符问题.
- 认知