sizeof()计算结构体的大小

转载自https://blog.csdn.net/qq_37858386/article/details/75909852

简要说明:结构体成员按照定义时的顺序依次存储在连续的内存空间,但是结构体的大小并不是简单的把所有成员大小相加,而是遵循一定的规则,需要考虑到系统在存储结构体变量时的地址对齐问题。

一、没有成员的结构体占用的空间是多少个字节

     答案是:1个字节。

     这就是实例化的原因(空类同样可以被实例化),每个实例在内存中都有一个独一无二的地址,为了达到这个目的,编译器往往会给一个空类或空结构体(C++中结构体也可看为类)隐含的加一个字节,这样空类或空结构体在实例化后在内存得到了独一无二的地址,所以空类所占的内存大小是1个字节。

二、首先介绍一个相关的概念——偏移量

          struct stru 
         {  
                    int a;  //start address is 0
                   char b;  //start address is 4
                   int c;  //start address is 8
         };

         偏移量指的是结构体变量中成员的地址和结构体变量地址的差。结构体大小等于最后一个成员的偏移量加上最后一个成员的大小。显然,结构体变量中第一个成员的地址就是结构体变量的首地址。比如上面的结构体,第一个成员a的偏移量为0。第二个成员b的偏移量是第一个成员的偏移量加上第一个成员的大小(0+4),其值为4;第三个成员c的偏移量是第二个成员的偏移量应该是加上第二个成员的大小(4+1)。

三、在实际中,存储变量时地址要求对齐,编译器在编译程序时会遵循两条原则:

             (1)结构体变量中成员的偏移量必须是成员大小的整数倍(0被认为是任何数的整数倍) 

     (2)结构体大小必须是所有成员大小的整数倍,也即所有成员大小的公倍数。

例子1:

struct stru1  
{  
     int a;  //start address is 0
     char b;  //start address is 4
     int c;  //start address is 8
};

PS:用sizeof求该结构体的大小,发现值为12。int占4个字节,char占1个字节,结果应该是9个字节才对啊,为什么呢?这个例子中前两个成员的偏移量都满足要求,但第三个成员的偏移量为5,并不是自身(int)大小的整数倍。编译器在处理时会在第二个成员后面补上3个空字节,使得第三个成员的偏移量变成8。结构体大小等于最后一个成员的偏移量加上其大小,上面的例子中计算出来的大小为12,满足要求。

例子2:

struct stru2  
{  
      int i;  //start address is 0
      short m;  //start address is 4
};

PS:成员i的偏移量为0;成员m的偏移量为4,都不需要调整。但计算出来的大小为6,显然不是成员m大小的整数倍。因此,编译器会在成员m后面补上2个字节,使得结构体的大小变成8从而满足第二个要求。

例子3、4:

 struct stru3  
{   
       char i;  //start address is 0 
       int m;   //start address is 4
       char n;  //start address is 8
};  
struct stru4  
{  
       char i;  //start address is 0
       char n;  //start address is 1
       int m;  //start address is 4
 }; 

虽然结构体stru3和stru4中成员都一样,但sizeof(struct stru3)的值为12而sizeof(struct stru4)的值为8。

由此可见,结构体类型需要考虑到字节对齐的情况,不同的顺序会影响结构体的大小。

对于嵌套的结构体,需要将其展开。对结构体求sizeof时,上述两种原则变为:

       (1)展开后的结构体的第一个成员的偏移量应当是被展开的结构体中最大的成员的整数倍。

       (2)结构体大小必须是所有成员大小的整数倍,这里所有成员计算的是展开后的成员,而不是将嵌套的结构体当做一个整体。

例子1:

struct stru5  
{  
      short i;  
      struct   
      {  
           char c;  
           int j;  
      } tt;   
      int k;  
};

结构体stru5的成员tt.c的偏移量应该是4,而不是2。整个结构体大小应该是16。

例子2:

struct stru6  
{  
      char i;  
      struct   
      {  
           char c;  
           int j;  
      } tt;   
      char a;  
      char b;  
      char d;  
      char e;  
      int f;  
};

结构体tt单独计算占用空间为8,而stru6的sizeof则是20,不是8的整数倍,这说明在计算sizeof(stru6)时,将嵌套的结构体ss展开了,这样stu6中最大的成员为tt.j,占用4个字节,20为4的整数倍。如果将tt当做一个整体,结果应该是24了。


五:另一个特殊的例子是结构体中包含数组,其sizeof应当和处理嵌套结构体一样,将其展开,如下例子:

struct array  
{  
    float f;  
    char p;  
    int  arr[3];  
};

其值为20。float占4个字节,到char p时偏移量为4,p占一个字节,到int arr[3]时偏移量为5,扩展为int的整数倍,而非int arr[3]的整数倍,这样偏移量变为8,而不是12。结果是8+12=20,是最大成员float或int的大小的整数倍。

测试环境:vc++6.0

测试代码:


  
  
  1. //#ifndef __cplusplus
  2. //#endif
  3. #include <iostream>
  4. #include "stdio.h"
  5. #include <stdlib.h>
  6. using namespace std;
  7. struct stru_empty
  8. {
  9. };
  10. struct stru1
  11. {
  12. int a; //start address is 0
  13. char b; //start address is 4
  14. int c; //start address is 8
  15. };
  16. struct stru2
  17. {
  18. int i; //start address is 0
  19. short m; //start address is 4
  20. };
  21. struct stru3
  22. {
  23. char i; //start address is 0
  24. int m; //start address is 4
  25. char n; //start address is 8
  26. };
  27. struct stru4
  28. {
  29. char i; //start address is 0
  30. char n; //start address is 1
  31. int m; //start address is 4
  32. };
  33. struct stru5
  34. {
  35. short i;
  36. struct
  37. {
  38. char c;
  39. int j;
  40. } ss;
  41. int k;
  42. };
  43. struct stru6
  44. {
  45. char i;
  46. struct
  47. {
  48. char c;
  49. int j;
  50. } tt;
  51. char a;
  52. char b;
  53. char d;
  54. char e;
  55. int f;
  56. };
  57. struct stru7
  58. {
  59. char i;
  60. struct
  61. {
  62. char c;
  63. //int j;
  64. } tt;
  65. char a;
  66. char b;
  67. char d;
  68. char e;
  69. int f;
  70. };
  71. struct array
  72. {
  73. float f;
  74. char p;
  75. int arr[ 3];
  76. };
  77. int main()
  78. {
  79. struct stru6 st6;
  80. struct stru7 st7;
  81. struct array ar;
  82. printf( "sizof(char)=%d \n", sizeof( char));
  83. printf( "sizof(int)=%d \n", sizeof( int));
  84. printf( "sizof(short int)=%d \n", sizeof( short int));
  85. printf( "sizof(long int)=%d \n", sizeof( long int));
  86. printf( "sizof(long)=%d \n", sizeof( long));
  87. printf( "sizof(float)=%d \n\n", sizeof( float));
  88. printf( "sizof(stru_empty)=%d \n", sizeof(stru_empty));
  89. printf( "sizof(stru1)=%d \n\n", sizeof(stru1));
  90. printf( "sizof(stru2)=%d \n\n", sizeof(stru2));
  91. printf( "sizof(stru3)=%d \n\n", sizeof(stru3));
  92. printf( "sizof(stru4)=%d \n\n", sizeof(stru4));
  93. printf( "sizof(stru5)=%d \n\n", sizeof(stru5));
  94. printf( "sizof(stru6)=%d \n", sizeof(stru6));
  95. printf( "sizof(stru6.tt)=%d \n", sizeof(st6.tt));
  96. printf( "the address of stru6.i=%d \n",&st6.i);
  97. printf( "the address of stru6.a=%d \n\n",&st6.a);
  98. printf( "sizof(stru7)=%d \n", sizeof(stru7));
  99. printf( "sizof(stru7)=%d \n", sizeof(st7.tt));
  100. printf( "the address of stru7.i=%d \n",&st7.i);
  101. printf( "the address of stru7.a=%d \n\n",&st7.a);
  102. printf( "sizof(ar)=%d \n", sizeof(ar));
  103. printf( "sizof(ar.f)=%d \n", sizeof(ar.f));
  104. printf( "sizof(ar.arr)=%d \n", sizeof(ar.arr));
  105. return 0;
  106. }
运行结果:



猜你喜欢

转载自blog.csdn.net/Melody1994/article/details/89930271
今日推荐