sizeof总结

一直都用sizeof和strlen,但是都没怎么太注意,直到有一次Socket编程时,把strlen的地方放上了sizeof 。。。不用说,程序BUG是跑不掉的了,现在认真的总结一番。

在MSDN中,sizeof的解释如下:

The sizeof keyword gives the amount ofstorage, in bytes, associated with a variable or a type (including aggregatetypes). This keyword returns a value of type size_t.

从这里,看出sizeof判断数据类型长度符的关键字,看实例吧:

1 sizeof(基本类型)

#include <iostream>
using namespace std;
typedef struct STUDENT1
{
	int no;
	double score;
	char name[4];
}stu1;

typedef struct STUDENT2
{
	int no;
	char name[4];
	double score;
}stu2;

typedef struct STUDENT3
{

}stu3;

int main()
{
	char ca;
	char cb='d';
	char *cc="hello sizeof()";
	char *cd=(char*)malloc(100);
	char ce[100];
	
	cout<<sizeof(ca)<<endl;//1 char类型,一个字节
	cout<<sizeof(cb)<<endl;//1 char类型,一个字节
	cout<<sizeof(cc)<<endl;//4 指针类型,四个字节
	cout<<sizeof(*cc)<<endl;//1 char类型,一个字节
	cout<<sizeof(char)<<endl;//1 char类型,一个字节
	cout<<sizeof(char*)<<endl;//4 指针类型,四个字节
	cout<<sizeof(cd)<<endl;	//4 指针类型,四个字节
	cout<<sizeof(*cd)<<endl;//1 char类型,一个字节
	cout<<sizeof(ce)<<endl;//100 数组,100个字节
	cout<<sizeof(*ce)<<endl;//1 char类型,一个字节
	
	cout<<sizeof(float)<<endl;//4 单精度,四个字节
	cout<<sizeof(double)<<endl;//8 双精度,八个字节
	cout<<sizeof(long)<<endl;//4 长整形,四个字节
	return 0;
}


2 sizeof(结构体)

#include <iostream>
using namespace std;
typedef struct STUDENT1
{
	int no;
	double score;
	char name[4];
}stu1;

typedef struct STUDENT2
{
	int no;
	char name[4];
	double score;
}stu2;

typedef struct STUDENT3
{

}stu3;

int main()
{
	cout<<sizeof(stu1)<<endl;//24 8+8+8
	cout<<sizeof(stu2)<<endl;//16 4+4+8
	cout<<sizeof(stu3)<<endl;//1  必须占一个字节
	cout<<sizeof(stu1*)<<endl;//4 指针,四个字节
	cout<<sizeof(stu2*)<<endl;//4 指针,四个字节
	cout<<sizeof(stu3*)<<endl;//4 指针,四个字节
	return 0;
}

这里需要说明一下,结构体中成员是自动补齐的。比如上面,stu1和stu2种成员相同,只是顺序不同导致结构体占字节不同。stu1中,int占4个字节,double占8个,char[4]占4个字节。但是结构体成员变量的顺序是int double char[4],例如第一个int变量开始的地址是 a,那么由于自动补齐,编译器会向最大的看齐,b开始的地址就是a+8。也就是说int理论占用内存4个字节地址,而实际上占用和double相同的8个地址,这就造成了sizeof(stu1)=8+8+8=24。而sut2中,int和char[4]挨着而且连个成员共占8个字节,所以实际占用的字节数是4+4+8=16个。

由于自动补齐的原因,定义结构体应注意此类问题,尽量不要造成内存浪费。

3 sizeof(类)

#include <iostream>
using namespace std;
class A//抽象类
{
public :
	virtual void test()=0;
	virtual void test1();
	void te();
};
void A::test1(){}
void A::te(){}

class B
{
private :
	int a;
	char b[4];
public:
	float c;
	double d;
};

class C:public A
{
public:
	virtual void test();
private:
	void reload(int);	
	void reload(double);
	void t();
	int d();
};
void C::test(){}
void C::reload(int){}
void C::reload(double){}
void C::t(){}
int C::d(){return 1;}

class D:public B
{
	int b;
};

int main()
{
	cout<<sizeof(A)<<endl;//4 如果有函数,则为4,
	cout<<sizeof(B)<<endl;//24 如果类中有数据,类大小为变量之和
	cout<<sizeof(C)<<endl;//4
	cout<<sizeof(D)<<endl;//32 如果类中有数据,类大小为父类变量之和+子类变量之和
	cout<<sizeof(A*)<<endl;//4 指针类型,都为4
	cout<<sizeof(B*)<<endl;//4 指针类型,都为4
	cout<<sizeof(C*)<<endl;//4 指针类型,都为4
	cout<<sizeof(D*)<<endl;//4 指针类型,都为4
	return 0;
}

类同结构体相似,注意继承子类及对象占用的大小是父类所有变量之和+子类自身变量之和。

注意:类中只有成员变量是占用空间的,成员函数是公用的。所以在上例A中,如果把虚函数去掉,则sizeof(A)=1
以上代码运行环境:32位/win7/vc6.0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值