美文网首页从汇编到C++
结构体 字节对齐的练习

结构体 字节对齐的练习

作者: Asura_Luo | 来源:发表于2018-05-04 02:47 被阅读0次

    结构体 字节对齐的练习

    1. 分析结构体大小

      struct S1                                                        
      {                                                                
              char c1;                                                         
              double d;
              char c2;
              char c3;                                                
      };
      struct S2                                                        
      {                                                                
              char c1;        
              char c2;
              char c3;                                                 
              double d;
                                                              
      };                
      struct S3                                
      {                
              char c1;                 
              S1 s; 
              char c2;
              char c3;
      };        
      
      image
    2. 分析结构体大小

      #pragma pack(1)
      struct S1                                                        
      {                                                                
              char c1;                                                         
              double d;
              char c2;
              char c3;                                                
      };
      #pragma pack()
      
      struct S3                                
      {                
              char c1;                 
              S1 s; 
              char c2;
              char c3;
      };
      
      image
    1. 分析结构体大小

      #pragma pack(4)
      struct S1                                                        
      {                                                                
              char c1;                                                         
              double d;
              char c2;
              char c3;                                                
      };
      #pragma pack()
      
      struct S3                                
      {                
              char c1;                 
              S1 s; 
              char c2;
              char c3;
      };
      
      image
    1. 分析结构体大小

      struct S1                                                        
      {                                                                
              char c1;                                                         
              double d;
              char c2;
              char c3;                                                
      };
      
      #pragma pack(1)
      struct S3                                
      {                
              char c1;                 
              S1 s; 
              char c2;
              char c3;
      };
      #pragma pack()
      
      
    ![image](https://img.haomeiwen.com/i5396392/b1feba86ffd81779.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    

    相关文章

      网友评论

        本文标题:结构体 字节对齐的练习

        本文链接:https://www.haomeiwen.com/subject/moyrrftx.html