zoukankan      html  css  js  c++  java
  • sizeof()计算结构体的大小

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

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

         答案是: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

    测试代码:


    //#ifndef __cplusplus

    //#endif


    #include <iostream>
    #include "stdio.h"
    #include <stdlib.h>
    using namespace std;

    struct stru_empty
    {

    };
    struct stru1
    {
    int a; //start address is 0
    char b; //start address is 4
    int c; //start address is 8
    };
    struct stru2
    {
    int i; //start address is 0
    short m; //start address is 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
    };

    struct stru5
    {
    short i;
    struct
    {
    char c;
    int j;
    } ss;
    int k;
    };
    struct stru6
    {
    char i;
    struct
    {
    char c;
    int j;
    } tt;
    char a;
    char b;
    char d;
    char e;
    int f;
    };
    struct stru7
    {
    char i;
    struct
    {
    char c;
    //int j;
    } tt;
    char a;
    char b;
    char d;
    char e;
    int f;
    };
    struct array
    {
    float f;
    char p;
    int arr[3];
    };
    int main()
    {
    struct stru6 st6;
    struct stru7 st7;
    struct array ar;
    printf("sizof(char)=%d ",sizeof(char));
    printf("sizof(int)=%d ",sizeof(int));
    printf("sizof(short int)=%d ",sizeof(short int));
    printf("sizof(long int)=%d ",sizeof(long int));
    printf("sizof(long)=%d ",sizeof(long));
    printf("sizof(float)=%d ",sizeof(float));

    printf("sizof(stru_empty)=%d ",sizeof(stru_empty));
    printf("sizof(stru1)=%d ",sizeof(stru1));
    printf("sizof(stru2)=%d ",sizeof(stru2));
    printf("sizof(stru3)=%d ",sizeof(stru3));
    printf("sizof(stru4)=%d ",sizeof(stru4));
    printf("sizof(stru5)=%d ",sizeof(stru5));
    printf("sizof(stru6)=%d ",sizeof(stru6));
    printf("sizof(stru6.tt)=%d ",sizeof(st6.tt));
    printf("the address of stru6.i=%d ",&st6.i);
    printf("the address of stru6.a=%d ",&st6.a);

    printf("sizof(stru7)=%d ",sizeof(stru7));
    printf("sizof(stru7)=%d ",sizeof(st7.tt));
    printf("the address of stru7.i=%d ",&st7.i);
    printf("the address of stru7.a=%d ",&st7.a);

    printf("sizof(ar)=%d ",sizeof(ar));
    printf("sizof(ar.f)=%d ",sizeof(ar.f));
    printf("sizof(ar.arr)=%d ",sizeof(ar.arr));
    return 0;
    }
    运行结果:

    ————————————————
    版权声明:本文为CSDN博主「海月汐辰」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_37858386/article/details/75909852

  • 相关阅读:
    Asp.net2.0中的提供程序dashcommerce中的税率计算提供程序
    C#中的位操作
    一个很不错的日历控件
    统计商品留言信息,列出 已读,未读和总数
    Linq学习笔记
    TSQL 选择某一记录的前后相关记录
    XPath简明语法
    统计下载的周排行跟月排行
    二进制度序列化碰到的问题 切实认识Asp.net2.0页面编译机制
    Asp.net中Web.config 页面访问按角色授权配置
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/11370836.html
Copyright © 2011-2022 走看看