结构体 字节对齐的练习

结构体 字节对齐的练习

  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](http://upload-images.jianshu.io/upload_images/5396392-b1feba86ffd81779.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(结构体 字节对齐的练习)