结构体的大小(内存对齐)

35 篇文章 0 订阅
#include<iostream>
using namespace std;
int main(int argc, char *argv[] )
{


    struct                             //C++可以有空结构体,在G++编译器定义为1字节
    {


    } my_student ;
    cout << "G++的空结构体大小"<< endl;
    cout << sizeof(my_student) << endl;






    int *p_int = NULL;            //测试本机器是64位的
    cout << "指针的大小:"<<endl;
    cout << sizeof (p_int )<<endl;






    struct student {
      
        char name;         // 1   +1
        short id;         //  2
        char flag;        //  1                        总共是5
    }my_student1; 
                     //因为自身类型最大值为2 ,系统的是8  ,所以最终有效值取最小值 2
    cout << "my_student1 的大小"<< endl;
    cout << sizeof(my_student1)<<endl;












    struct stdent1 {
         char name;   //1    +1
         short id;   // 2    
         int number;  // 4
         char str;    //1       自身类型最大值是4 ,系统是8  ,故最终有效最小值为4   //一共是12
    }my_student2;
    cout << "my_student2 的大小"<<endl;
    cout << sizeof(my_student2)<< endl;










    struct  teacher{
        int a ;      //4  
        char b;      //1  +3     //注意:是类型对齐,将下一个结构体看成一个块,下一个结构体类型最大值为4,系统为8
        struct teacher4{         //      则有效对齐值为4,所以加 +3    总为8
            int id;        //4
            char name;     //1  +1
            short number;   //         结构体 总共是8 ,但要自身对齐,同理,对齐有效值为4    总为8
        }teacher4;
        short c;                          // 18          //总为20  同理 自身有效值为4  //所以最终为20
    }teacher1;
    cout<< "teacher1 的大小"<< endl;
    cout << sizeof(teacher1)<< endl;






                                             
    struct  teach{                   //这是一道陷阱题  
        int a ;      //4  
        char b;      //1   +1
        struct teacher4{           //里面的结构体teacher4是一个声明,不占空间
            int id;        
            char name;     
            short number;        
        };
        short c;                 //  总共为8 有效对齐值为4  所以最终为8 
    }teacher6;
    cout<< "teacher6 的大小"<< endl;
    cout << sizeof(teacher6)<< endl;














    struct  tea{
        int a ;      //4  
        char b;      //1  +3 
        struct {                                       //这个分析与teacher1一样  //注意struct 的标签,定义都没有
            int id;        //4
            char name;     //1  +1
            short number;   //          总共是8  
        };
        short c;                          // 18          //20
    }teacher5;
    cout<< "teacher5 的大小"<< endl;
    cout << sizeof(teacher5)<< endl;












    struct  mabo{
        char str[10];   //10  +1     相当与10个char 类型,最后一个加一// 注意是类型对齐 //大小为  11
        short  id ;     //2           //12
        char str1[20];  //  str1看成一个块,类似结构体的处理方法,12 + 20  = 32
        int a;          //4       //36
    }mabo1;
    cout << "mobo1 的大小"<<endl;
    cout << sizeof(mabo1)<< endl;
    return 0;

}




#include<iostream>
using namespace std;
int main(int argc, char *argv[] )
{

    struct                             //C++可以有空结构体,在G++编译器定义为1字节
    {

    } my_student ;
    cout << "G++的空结构体大小"<< endl;
    cout << sizeof(my_student) << endl;



    int *p_int = NULL;            //测试本机器是64位的
    cout << "指针的大小:"<<endl;
    cout << sizeof (p_int )<<endl;



    struct student {
      
        char name;         // 1   +1
        short id;         //  2
        char flag;        //  1                        总共是5
    }my_student1; 
                     //因为自身类型最大值为2 ,系统的是8  ,所以最终有效值取最小值 2
    cout << "my_student1 的大小"<< endl;
    cout << sizeof(my_student1)<<endl;






    struct stdent1 {
         char name;   //1    +1
         short id;   // 2    
         int number;  // 4
         char str;    //1       自身类型最大值是4 ,系统是8  ,故最终有效最小值为4   //一共是12
    }my_student2;
    cout << "my_student2 的大小"<<endl;
    cout << sizeof(my_student2)<< endl;





    struct  teacher{
        int a ;      //4  
        char b;      //1  +3     //注意:是类型对齐,将下一个结构体看成一个块,下一个结构体类型最大值为4,系统为8
        struct teacher4{         //      则有效对齐值为4,所以加 +3    总为8
            int id;        //4
            char name;     //1  +1
            short number;   //         结构体 总共是8 ,但要自身对齐,同理,对齐有效值为4    总为8
        }teacher4;
        short c;                          // 18          //总为20  同理 自身有效值为4  //所以最终为20
    }teacher1;
    cout<< "teacher1 的大小"<< endl;
    cout << sizeof(teacher1)<< endl;



                                             
    struct  teach{                   //这是一道陷阱题  
        int a ;      //4  
        char b;      //1   +1
        struct teacher4{           //里面的结构体teacher4是一个声明,不占空间
            int id;        
            char name;     
            short number;        
        };
        short c;                 //  总共为8 有效对齐值为4  所以最终为8 
    }teacher6;
    cout<< "teacher6 的大小"<< endl;
    cout << sizeof(teacher6)<< endl;







    struct  tea{
        int a ;      //4  
        char b;      //1  +3 
        struct {                                       //这个分析与teacher1一样  //注意struct 的标签,定义都没有
            int id;        //4
            char name;     //1  +1
            short number;   //          总共是8  
        };
        short c;                          // 18          //20
    }teacher5;
    cout<< "teacher5 的大小"<< endl;
    cout << sizeof(teacher5)<< endl;






    struct  mabo{
        char str[10];   //10  +1     相当与10个char 类型,最后一个加一// 注意是类型对齐 //大小为  11
        short  id ;     //2           //12
        char str1[20];  //  str1看成一个块,类似结构体的处理方法,12 + 20  = 32
        int a;          //4       //36
    }mabo1;
    cout << "mobo1 的大小"<<endl;
    cout << sizeof(mabo1)<< endl;
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值