sizeof


定义:sizeof是Pascal的一种内存容量度量函数。C语言中判断数据类型或者表达式长度的运算符;不是一个函数,字节(byte)数的计算在程序编译时进行,而不是在程序执行的过程中才计算出来。

1:字节、位、字长

字 word
字节 byte
位 bit

1字=2字节(1 word = 2 byte)
1字节=8位(1 byte = 8bit)
 
一个字的字长为16(bit)
一个字节的字长是8(bit)

2.基本类型

2.1:整型

int main()
{
        short a,aa[10];
	int b ,bb[10];
	long c,cc[10];
	long long d,dd[10];
	cout << "sizeof(a)=" << sizeof(a) << endl;
	cout << "sizeof(b)=" << sizeof(b) << endl;
	cout << "sizeof(c)=" << sizeof(c) << endl;
	cout << "sizeof(d)=" << sizeof(d) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
	cout << "sizeof(bb)=" << sizeof(bb) << endl;
	cout << "sizeof(cc)=" << sizeof(cc) << endl;
	cout << "sizeof(dd)=" << sizeof(dd) << endl;
}

2.2:浮点型

int main()
{
        float a,aa[10];
	double b,bb[10];
	long double c,cc[10];
	cout << "sizeof(a)=" <<sizeof(a) << endl;
	cout << "sizeof(b)=" << sizeof(b) << endl;
	cout << "sizeof(c)=" << sizeof(c) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
	cout << "sizeof(bb)=" << sizeof(bb) << endl;
	cout << "sizeof(cc)=" << sizeof(cc) << endl;
}

2.3:字符

int main ()
{ 
        char a,aa[10];
	cout << "sizeof(a)=" << sizeof(a) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
}

2.4:布尔

int main()
{
	bool a, aa[10];
	cout << "sizeof(a)=" << sizeof(a) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
}

3:指针


3.1:整型

int main()
{
        short *a,*aa[10];
	int *b ,*bb[10];
	long *c,*cc[10];
	long long *d,*dd[10];
	cout << "sizeof(a)=" <<sizeof(a) << endl;
	cout << "sizeof(b)=" << sizeof(b) << endl;
	cout << "sizeof(c)=" << sizeof(c) << endl;
	cout << "sizeof(d)=" << sizeof(d) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
	cout << "sizeof(bb)=" << sizeof(bb) << endl;
	cout << "sizeof(cc)=" << sizeof(cc) << endl;
	cout << "sizeof(dd)=" << sizeof(dd) << endl;
}



3.2:浮点型

int main ()
{
        float *a,*aa[10];
	double *b,*bb[10];
	long double *c,*cc[10];
	cout << "sizeof(a)=" <<sizeof(a) << endl;
	cout << "sizeof(b)=" << sizeof(b) << endl;
	cout << "sizeof(c)=" << sizeof(c) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
	cout << "sizeof(bb)=" << sizeof(bb) << endl;
	cout << "sizeof(cc)=" << sizeof(cc) << endl;
}


3.3:字符型

int main()
{
        char *a,*aa[10];
	cout << "sizeof(a)=" << sizeof(a) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
}

3.4:布尔型

int main()
{
        bool *a, *aa[10];
	cout << "sizeof(a)=" << sizeof(a) << endl;
	cout << "sizeof(aa)=" << sizeof(aa) << endl;
}

4:结构体和类

4.1:结构体

    #include <stdio.h>  
    #include <iostream>  
    using namespace std;  
    typedef struct    
    {  
        char a:3;  
        char b:3;  
        char c:3;  
        char d:3;  
        char e:3;  
    }test1;  
    typedef struct    
    {  
        char a:3;  
        char b:4;  
        char c:5;  
        char d:6;  
        char e:7;  
    }test2;  
    typedef struct    
    {  
        char a:1;  
        char b:2;  
        char c:3;  
        char d:4;  
        char e:5;  
    }test3;  
    typedef struct{  
        int a;  
        char b;  
        char d;  
        long c;  
    }test4;  
    typedef struct{  
        int a;  
        char b;  
        long c;  
        char d;  
    }test5;  
      
      
    int main()  
    {  
        cout<<sizeof(test1)<<endl;  
        cout<<sizeof(test2)<<endl;  
        cout<<sizeof(test3)<<endl;  
        cout<<sizeof(test4)<<endl;  
        cout<<sizeof(test5)<<endl;  
        return 0;  
    }  

4.2:类

#include <stdio.h>  
#include <iostream>  
using namespace std;
class emptyClass1{
public:
	emptyClass1(){}
	~emptyClass1(){}
};
class emptyClass2{
public:
	emptyClass2(){}
	virtual ~emptyClass2(){}
};
class hwcBase{
public:
	hwcBase(){}
	virtual  ~hwcBase(){}
private:
	int base;
};

class hwcSubFirst :hwcBase{
public:
	hwcSubFirst() :hwcBase(){}
	~hwcSubFirst(){}
private:
	int sub;
};

class hwcSubSecond :hwcBase{
public:
	hwcSubSecond() :hwcBase(){}
	~hwcSubSecond(){}
private:
	int sub;
	char sub2;
};

int main()
{
	cout << sizeof(emptyClass1) << endl;
	cout << sizeof(emptyClass2) << endl;
	cout << sizeof(hwcBase) << endl;
	cout << sizeof(hwcSubFirst) << endl;
	cout << sizeof(hwcSubSecond) << endl;
	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Simon|

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

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

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

打赏作者

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

抵扣说明:

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

余额充值