sizeof面试题

原创Blog,转载请注明处处

 http://blog.csdn.net/hello_hwc

注意:sizeof是编译期计算出结果的,这一点对后面的理解很重要

一、关于结构体

先看下代码

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4. typedef struct    
  5. {  
  6.     char a:3;  
  7.     char b:3;  
  8.     char c:3;  
  9.     char d:3;  
  10.     char e:3;  
  11. }test1;  
  12. typedef struct    
  13. {  
  14.     char a:3;  
  15.     char b:4;  
  16.     char c:5;  
  17.     char d:6;  
  18.     char e:7;  
  19. }test2;  
  20. typedef struct    
  21. {  
  22.     char a:1;  
  23.     char b:2;  
  24.     char c:3;  
  25.     char d:4;  
  26.     char e:5;  
  27. }test3;  
  28. typedef struct{  
  29.     int a;  
  30.     char b;  
  31.     char d;  
  32.     long c;  
  33. }test4;  
  34. typedef struct{  
  35.     int a;  
  36.     char b;  
  37.     long c;  
  38.     char d;  
  39. }test5;  
  40.   
  41.   
  42. int _tmain(int argc, _TCHAR* argv[])  
  43. {  
  44.     cout<<sizeof(test1)<<endl;  
  45.     cout<<sizeof(test2)<<endl;  
  46.     cout<<sizeof(test3)<<endl;  
  47.     cout<<sizeof(test4)<<endl;  
  48.     cout<<sizeof(test5)<<endl;  
  49.     return 0;  
  50. }  
然后贴上运行结果


解释下为什么

1、对于test1,结果是3Byte。有些同学好奇,3bit*5 = 15bit,不是应该占用2Byte吗?可是计算机没你大脑智能,它管理bit的方式是8bit一组,所以对于一组8bit,只能放下两个3bit,另外2bit没用。所以,对于test1来说,三个Byte是这么分配的。1Byte(3bit,3bit,2bit填充)2Byte(3bit,3bit,2bit填充)3Byte(3bit,5bit填充)

2、对于test2结果是4Byte.有了test1的解释,这里不难理解了吧。我再解释下内存分配。1Byte(3bit,4bit,1bit填充)2Byte(5bit,3bit填充)3Byte(6bit,2bit填充)4Byte(7bit,1bit填充)

3、test3结果是3Byte。和上述两个类似

4、test4的结果是12。为什么不是4+1+1+4呢?(我这里long是4Byte),因为C++对于Byte的管理仍然要内存对齐,我这里的内存对其是4Byte。所以,内存占用是这样的4Byte(Int)4Byte(char,char,2Byte填充)4Byte(long)

5、有些同学会好奇,为什么两个结构体声明换了个顺序,差了4Byte。同样是因为内存对齐


二、字符串与char*和char数组

[cpp]  view plain copy
  1. // testforyou.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     string s = "hellohwc";  
  13.     char * s1 = "hellohwc";  
  14.     char s2[] = "hellohwc";  
  15.     char s3[100];  
  16.     char* s4=(char*)malloc(100);  
  17.     void *s5=(void*)malloc(100);  
  18.     cout<<sizeof(s)<<endl;  
  19.     cout<<sizeof(s1)<<endl;  
  20.     cout<<sizeof(s2)<<endl;  
  21.     cout<<sizeof(s3)<<endl;  
  22.     cout<<sizeof(s4)<<endl;  
  23.     cout<<sizeof(s5)<<endl;  
  24.     return 0;  
  25. }  
运行结果


解释下

对于String类型。每个编译器给分配内存的空间都是一定的,我这里是32Byte。所以,不管字符串长度多长,内存分配都是32。当然也有4,16等情况,因编译期不同而不同。

1、char * s1,s1本身是个指针,所以长度是4个字节,跟后面指向什么没关系。

2、s2是数组保存了一个字符串。由于字符串最后一个隐藏的结束符。所以,长度为8+1 = 9

3、s3是数组,在编译期分配了100*1 = 100Byte,所以结果是100

4、s4和s5都是指针,长度4Byte。

三、类

先看代码

[cpp]  view plain copy
  1. // testforyou.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9. class emptyClass1{  
  10. public:  
  11.     emptyClass1(){}  
  12.     ~emptyClass1(){}  
  13. };  
  14. class emptyClass2{  
  15. public:  
  16.     emptyClass2(){}  
  17.     virtual ~emptyClass2(){}  
  18. };  
  19. class hwcBase{  
  20. public:  
  21.     hwcBase(){}  
  22.     virtual  ~hwcBase(){}  
  23. private:  
  24.     int base;  
  25. };  
  26.   
  27. class hwcSubFirst:hwcBase{  
  28. public:  
  29.     hwcSubFirst():hwcBase(){}  
  30.     ~hwcSubFirst(){}  
  31. private:  
  32.     int sub;  
  33. };  
  34.   
  35. class hwcSubSecond:hwcBase{  
  36. public:  
  37.     hwcSubSecond():hwcBase(){}  
  38.     ~hwcSubSecond(){}  
  39. private:  
  40.     int sub;  
  41.     char sub2;  
  42. };  
  43.   
  44. int _tmain(int argc, _TCHAR* argv[])  
  45. {  
  46.     cout<<sizeof(emptyClass1)<<endl;  
  47.     cout<<sizeof(emptyClass2)<<endl;  
  48.     cout<<sizeof(hwcBase)<<endl;  
  49.     cout<<sizeof(hwcSubFirst)<<endl;  
  50.     cout<<sizeof(hwcSubSecond)<<endl;  
  51.     return 0;  
  52. }  

运行结果


解释下:

1、对于一个空的类,在内存中要存在一个标示来区分,所以即使是空的,也要分配一个字节

2、同样是empty的类,但是有一个虚的析构函数,所以,保存了一个指针(vptr)指向虚表。一个指针4Byte

3、hwcBase类,有一个Int占用4Byte,一个指针(vptr),所以共占用8Byte

3、hwcSubFirst,继承来一个Int,本身有一个Int,加上一个vptr指针,共12字节

4、hwcSubSecond,和hwcSubFirst类似,但是多了一个char,考虑到内存对其,12+4 = 16字节

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值