C ++ Builder 2007,Union和位字段

时间:2013-02-21 11:35:24

标签: c++ c++builder sizeof unions bit-fields

此联合的大小返回16个字节(在C ++ Builder 2007中)。

typedef union
{
  struct
  {
    unsigned Type:2; 
    unsigned Prev:31;
    unsigned Next:31;
    unsigned SizeInBytes:32;
  };
} eMyUnion;

如果我想要sizeof(eMyUnion)= 12个字节,我必须如何修改这个联合定义? (我想保留这些字段及其大小,但可以重新排序) 有可能吗?

1 个答案:

答案 0 :(得分:1)

#pragma pack(push, 1)
  struct
  {
    unsigned Type:2; 
    unsigned Prev:31;
    unsigned Next:31;
    unsigned SizeInBytes:32;
  };
#pragma pack(pop)