关于sizeof的问题

wpc320 2008-10-15 08:37:52
#include <iostream>
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte

};
struct B:public A{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};

union{
unsigned short m;
unsigned int c;
}xx;
short myfun();
int (__stdcall *funs)(); //4 byte
struct C{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
int main()
{
cout << sizeof(myfun()) << endl;//2
cout << sizeof(( *funs)()) << endl;//4
cout << sizeof(xx) << endl;//4
cout << sizeof(A) << endl;//8
cout << sizeof(B) << endl;//16
cout << sizeof(C) << endl;//8
return 0;
}
//为啥sizeof(B)是16而不是18??
...全文
86 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpc320 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Demon__Hunter 的回复:]
引用 6 楼 wpc320 的回复:
引用 3 楼 lann64 的回复:
short myfun(); //2 byte 这个是函数,不占位置的。
sizeof(B)里,不含这个。

能告诉一下为什么呢

非虚函数地址编译期已确定,编译器负责绑定到B类型的上,所以不虚要在类对象内再存储相关的信息
[/Quote]
虽然不懂,谢谢
机智的呆呆 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wpc320 的回复:]
引用 3 楼 lann64 的回复:
short myfun(); //2 byte 这个是函数,不占位置的。
sizeof(B)里,不含这个。

能告诉一下为什么呢
[/Quote]
非虚函数地址编译期已确定,编译器负责绑定到B类型的上,所以不虚要在类对象内再存储相关的信息
wpc320 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 akirya 的回复:]
引用 9 楼 wpc320 的回复:
谢谢,又学了一手,通过偏移量可以看到,myfun()的确不占位置,但是还是不知道为什么


看深入探索C++对象模型
[/Quote]
谢谢
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wpc320 的回复:]
谢谢,又学了一手,通过偏移量可以看到,myfun()的确不占位置,但是还是不知道为什么
[/Quote]

看深入探索C++对象模型
wpc320 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 akirya 的回复:]
用offsetof看看个成员的偏移量就知道了

C/C++ code#include <iostream>
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte

};
struct B:public A{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};

union{
unsigned short m;
unsigned int c;
}xx…
[/Quote]
谢谢,又学了一手,通过偏移量可以看到,myfun()的确不占位置,但是还是不知道为什么
wpc320 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 akirya 的回复:]
用offsetof看看个成员的偏移量就知道了

C/C++ code#include <iostream>
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte

};
struct B:public A{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};

union{
unsigned short m;
unsigned int c;
}xx…
[/Quote]
谢谢,又学了一手,通过偏移量可以看到,myfun()的确不占位置,但是还是不知道为什么
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wpc320 的回复:]
引用 3 楼 lann64 的回复:
short myfun(); //2 byte 这个是函数,不占位置的。
sizeof(B)里,不含这个。

能告诉一下为什么呢
[/Quote]
函数不占用类对象的大小
  • 打赏
  • 举报
回复
用offsetof看看个成员的偏移量就知道了
#include <iostream>
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte

};
struct B:public A{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};

union{
unsigned short m;
unsigned int c;
}xx;
short myfun();
int (__stdcall *funs)(); //4 byte
struct C{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
int main()
{
cout << sizeof(myfun()) << endl;//2
cout << sizeof(( *funs)()) << endl;//4
cout << sizeof(xx) << endl;//4
cout << sizeof(A) << endl;//8
cout << sizeof(B) << endl;//16
cout << sizeof(C) << endl;//8

cout<<endl;
cout<<offsetof( B , x )<<endl;
cout<<offsetof( B , xx )<<endl;
cout<<offsetof( B , funs )<<endl;
return 0;
}
wpc320 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lann64 的回复:]
short myfun(); //2 byte 这个是函数,不占位置的。
sizeof(B)里,不含这个。
[/Quote]
能告诉一下为什么呢
wpc320 2008-10-15
  • 打赏
  • 举报
回复
达人来助我啊,是在vc2008中编译的
lann64 2008-10-15
  • 打赏
  • 举报
回复
short myfun(); //2 byte 这个是函数,不占位置的。
sizeof(B)里,不含这个。
newcore 2008-10-15
  • 打赏
  • 举报
回复
Memroy alignment
lann64 2008-10-15
  • 打赏
  • 举报
回复
内存布局?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧