空类、含静态成员的类、普通类(不含虚函数、虚继承)的sizeof大小

22 篇文章 0 订阅
6 篇文章 0 订阅

空类、含静态成员的类、普通类(不含虚函数、虚继承)的sizeof大小

#include <iostream>
using namespace std;

class CEmpty
{
};

class CStaticMember
{
static int a;
};

///

class CCharShortChar
{
char a;
short b;
char c;
};

class CCharCharShort
{
char a;
char c;
short b;
};

///

#pragma pack(1)
class CCharShortCharPack1
{
char a;
short b;
char c;
};

class CCharCharShortPack1
{
char a;
char c;
short b;
};
#pragma pack()

///

class CCharInt
{
char a;
int b;
};

class CCharDouble
{
char a;
double b;
};

class CCharShortIntDouble
{
public:
char a;
short b;
int c;
double d;
};

int main()
{
cout << "sizeof(CEmpty) = " << sizeof(CEmpty) << endl;
cout << "sizeof(CStaticMember) = " << sizeof(CStaticMember) << endl;
cout << "sizeof(CCharShortChar) = " << sizeof(CCharShortChar) << endl;
cout << "sizeof(CCharCharShort) = " << sizeof(CCharCharShort) << endl;
cout << "sizeof(CCharShortCharPack1) = " << sizeof(CCharShortCharPack1) << endl;
cout << "sizeof(CCharCharShortPack1) = " << sizeof(CCharCharShortPack1) << endl;
cout << "sizeof(CCharInt) = " << sizeof(CCharInt) << endl;
cout << "sizeof(CCharDouble) = " << sizeof(CCharDouble) << endl;

CCharShortIntDouble csid;
cout << "sizeof(CCharShortIntDouble) = " << sizeof(CCharShortIntDouble) << endl;
cout << "b - a = " << (int)&csid.b - (int)&csid.a << endl;
cout << "c - b = " << (int)&csid.c - (int)&csid.b << endl;
cout << "d - c = " << (int)&csid.d - (int)&csid.c << endl;
return 0;
}

//[VC2008运行结果]
//sizeof(CEmpty) = 1
//sizeof(CStaticMember) = 1
//sizeof(CCharShortChar) = 6
//sizeof(CCharCharShort) = 4
//sizeof(CCharShortCharPack1) = 4
//sizeof(CCharInt) = 8
//sizeof(CCharDouble) = 16 # 这一行VC2008和GCC结果不一致
//sizeof(CCharShortIntDouble) = 16
//b - a = 2
//c - b = 2
//d - c = 4
//请按任意键继续. . .


//[GCC 4.4.5 运行结果]
//sizeof(CEmpty) = 1
//sizeof(CStaticMember) = 1
//sizeof(CCharShortChar) = 6
//sizeof(CCharCharShort) = 4
//sizeof(CCharShortCharPack1) = 4
//sizeof(CCharInt) = 8
//sizeof(CCharDouble) = 12 # 这一行VC2008和GCC结果不一致
//sizeof(CCharShortIntDouble) = 16
//b - a = 2
//c - b = 2
//d - c = 4

//[备注]
//1, 空类的sizeof为1, 空类也能实例化, 而实例化的对象都有内存的地址, 因此最少要1个字节来表示实例在对象中的位置.
//2, 静态成员不占用sizeof的大小, 因为静态类成员是存储在静态存储区, 所有的实例和类共享.
//3, 相同的类成员, 不同的内部排列方式由可能会导致sizeof不同, 如上述的CCharShortChar和CCharCharShort, 具体大小和内存对齐布局的时候有关.
//4, 如果指定了对齐尺寸#pragma pack(n), 则内存布局按照n字节对齐. 恢复编译器默认对齐则使用#pragma pack().
//5, 默认的内存对齐尺寸和成员的尺寸以及编译器的设置相关, 如上述的CCharDouble在VC和GCC下sizeof不同. (测试发现VC中按照成员最大值对齐, GCC按照min(系统默认的4, 成员最大值)对齐)

包含虚函数或者是虚基类的类,由于需要存储虚函数表指针和虚基类表指针,sizeof值会有所增加,但是不同的编译器实现不同,sizeof的值也不一定相同(比如虚继承的类VC和GCC中的sizeof不同),这个后续再实验和总结。

手工计算sizeof本来没有多大的意义,直接在程序中使用sizeof计算就好。讨论手工计算sizeof的意义最主要的是了解部分内部的细节以及C++的设计思想。

另外,最好不要对类实例使用memset,因为如果类中包含了虚函数或者是虚继承,那会导致虚函数表指针或者虚基类指针破坏而让程序crash。

相关阅读:

http://hi.baidu.com/luosiyong/blog/item/331aa8ee0f159f092df5341e.html

http://hi.baidu.com/luosiyong/blog/item/2073c4dcfc2b1f81cd1166fe.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值