struct的data对齐

转自:

http://blog.csdn.net/stevenliyong/archive/2009/01/07/3726479.aspx

 

1. 编译器通常会使结构体强制对齐

 

对于结构体

struct struct_x {
   char a;     // 1 byte
   int b;      // 4 bytes
   short c;    // 2 bytes
   char d;     // 1 byte
} ;

 

编译器会自动插入一些pad

 

struct struct_x {
   char a;            // 1 byte
   char _pad0[3];     // padding to put 'b' on 4-byte boundary
   int b;            // 4 bytes

   short c;          // 2 bytes
   char d;           // 1 byte
   char _pad1[1];    // padding to make sizeof(x_) multiple of 4
}

 

所以sizeof(struct_x) 的返回值会是 12 bytes.

 

char _pad0[3] 使 int b 成员 4字节对齐;

(这里如果是double b;的话,因为double类型长是8字节的,所以变量b会被编译器强制8 bytes对齐

这样就需要插入char_pad0[7] 了);

 

char _pad1[1] 使结构体每个成员都对齐;

(试想一下,如果这个字节不插入, 则定义类似结构体数组 strcut my_struct x[3] 的时候,x[1]就无法对齐了.)

 

2. 如果嵌套定义结构体的话,编译器会自动优化对齐.

对于结构体

struct struct_x

{

    char xa;    // 1 byte

    int xb;      // 4 bytes

}

 

sizeof(struct_x) 的返回值会是 8 bytes.

 

struct struct_y
{
   char ya;     // 1 byte
   struct struct_x yb // 8 bytes

   double c;  // 8 bytes

   char d;     // 1 byte
} ;


struct_y 会不会被编译器对齐成这样呢?

struct struct_y
{
   char ya;     // 1 byte

   char _pad0[7];     
   struct struct_x yb // 8 bytes

   double c;  // 8 bytes

   char d;     // 1 byte

   char _pad1[7];      
} ;

 

使sizeof(struct_y) 的返回值会是 32 bytes. 呢?

答案是否定的,编译器会优化成

 

struct struct_y
{
   char ya;     // 1 byte

   struct struct_x {

      char xa;    // 1 byte

      char _pad0[2];       // padding to put 'xb' on 4-byte boundary

      int xb;      // 4 bytes

   } yb;

   double c;  // 8 bytes  // Here c is alreay 8-byte boundary,so no need to pad.

   char d;     // 1 byte

   char _pad1[7];       // padding to make sizeof(x_) multiple of 8
} ;

因此sizeof(struct_y) 的返回值会是 24 bytes!!

 

3. 结构体packet 化.

struct_x强制 模1 对齐.

# pragma pack (1)
struct struct_x
{
   char a;    // 1 byte
   int b;     // 4 bytes
   short c;   // 2 bytes
} ;
# pragma pack ()

sizeof(struct_x) 的返回值会是 7 bytes!!

 

struct_x被强制 模2 对齐.

# pragma pack (2)
struct struct_x
{
   char a;    // 1 byte
   int b;     // 4 bytes
   short c;   // 2 bytes
} ;
# pragma pack ()

相当于

struct struct_x
{
   char a;    // 1 byte

   char _pad0[1];       // padding to put 'b' on 2-byte boundary
   int b;     // 4 bytes  // b 被强制 模2 对齐了
   short c;   // 2 bytes
} ;

 

因此 sizeof(struct_x) 的返回值会是 8 bytes!!

 

struct_x被强制 模8 对齐.

# pragma pack (8)
struct struct_x
{
   char a;    // 1 byte
   int b;     // 4 bytes
   short c;   // 2 bytes
} ;
# pragma pack ()

相当于

struct struct_x
{
   char a;    // 1 byte

   char _pad0[3];       // padding to put 'b' on 4-byte boundary ,.
   int b;     // 4 bytes , int 类型 模8 ,b 还是按4 bytes  对齐
   short c;   // 2 bytes
} ;

 

因此 sizeof(struct_x) 的返回值会是 8 bytes!!

 

 Done

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值