【sizeof】关于各种东西的大小_Tencent

</pre><p></p><p></p><p>之前腾讯模拟笔试的时候,出了一道sizeof的题目,问没有规定大小的各种数据类型指针的size,于是这次还是写个文章测试顺便备忘一下。</p><p>测试代码:</p><p></p><pre code_snippet_id="628654" snippet_file_name="blog_20150326_2_2178915" name="code" class="cpp">#include <map>
#include <cmath>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring> 
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
	int *a,b;
	float *c,d;
	double *e,f;
	char *g,h;
	map<int,int> mii;
	map<double,double> mdd;
	map<char,char> mcc;
	map<string,int> msi;
	vector<int> vi;
	vector<double> vd;
	string *s,str;
	
	cout<<"int* \t"<<sizeof a<<endl;
	cout<<"int \t"<<sizeof b<<endl;
	cout<<"float* \t"<<sizeof c<<endl;
	cout<<"float \t"<<sizeof d<<endl;
	cout<<"double* "<<sizeof e<<endl;
	cout<<"double \t"<<sizeof f<<endl;
	cout<<"char* \t"<<sizeof g<<endl;
	cout<<"char \t"<<sizeof h<<endl;
	cout<<"mii \t"<<sizeof mii<<endl;
	cout<<"mdd \t"<<sizeof mdd<<endl;
	cout<<"mcc \t"<<sizeof mcc<<endl;
	cout<<"msi \t"<<sizeof msi<<endl;
	cout<<"veci \t"<<sizeof vi<<endl;
	cout<<"vecd \t"<<sizeof vd<<endl;
	cout<<"str* \t"<<sizeof s<<endl;
	cout<<"str \t"<<sizeof str<<endl;
	return 0; 
} 



结果

int*    8
int     4
float*  8
float   4
double* 8
double  8
char*   8
char    1
mii     48
mdd     48
mcc     48
msi     48
veci    24
vecd    24
str*    8
str     8




#include<iostream>

class A
{
};

class B
{
private:
	int a;
};
class B1
{
private:
	void fun()
	{}
};


class C
{
	virtual void fun() = 0;
};

class D
{
	virtual void fun()
	{
		std::cout << "this is a D1.";
	}
};

class D1
{
	virtual void fun()
	{
		std::cout << "this is a D2.";
	}
	virtual void fun1()
	{
		std::cout << "this is a D2.";
	}
};

class D2:public D1,public D
{
	virtual void fun()
	{
		std::cout << "this is a D3.";
	}
	virtual void fun1()
	{
		std::cout << "this is a D3.";
	}
};

class D3 :public D1, public D
{
	virtual void fun3()
	{
		std::cout << "this is a D3.";
	}
	
};
class D4 :public D1, public B1
{
	virtual void fun3()
	{
		std::cout << "this is a D3.";
	}

};


class E 
{
	void show()
	{
		std::cout << "THIS IS E";
	}
};

class F :public A
{
	virtual void show()
	{
		std::cout << "THIS IS E";
	}
};


int main()
{
	std::cout << "empty class's size is " << sizeof(A) << std::endl;
	std::cout << "only one int class's size is " << sizeof(B) << std::endl;
	std::cout << "only one funciton class's size is " << sizeof(B1) << std::endl;
	std::cout << "only one virtual pure funciton class's size is " << sizeof(C) << std::endl;
	std::cout << "only one virtual function class's size is " << sizeof(D) << std::endl;
	std::cout << "two virtual function class's size is " << sizeof(D1) << std::endl;

	std::cout << "three virtual function two jicheng(TWO VIRTUAL) class's size is " << sizeof(D2) << std::endl;
	std::cout << "three virtual function two jicheng(ONE VIRTUAL) class's size is " << sizeof(D4) << std::endl;
	std::cout << "four virtual function two jicheng class's size is " << sizeof(D3) << std::endl;

	std::cout << "only one  function class's size is " << sizeof(E) << std::endl;
	std::cout << "only one  virtual function class's size is " << sizeof(E) << std::endl;
	getchar();


}

反正写了也是写了,再看看类吧


empty class's size is 1
only one int class's size is 4
only one funciton class's size is 1
only one virtual pure funciton class's size is 8
only one virtual function class's size is 8
two virtual function class's size is 8
three virtual function two jicheng(TWO VIRTUAL) class's size is 16
three virtual function two jicheng(ONE VIRTUAL) class's size is 8
four virtual function two jicheng class's size is 16
only one  function class's size is 1
only one  virtual function class's size is 1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值