Struct 为什么这个typedef给出的sizeof()值大于预期值?

Struct 为什么这个typedef给出的sizeof()值大于预期值?,struct,sizeof,unions,bit-fields,xc16,Struct,Sizeof,Unions,Bit Fields,Xc16,我使用这种形式的typedef来简化对微处理器寄存器及其位字段的访问 typedef union { uint8_t u8Byte; ///< REG_8 as unsigned byte int8_t i8Byte; ///< REG_8 as signed byte struct { unsigned b0:1; //

我使用这种形式的typedef来简化对微处理器寄存器及其位字段的访问

  typedef union
  {
     uint8_t        u8Byte;           ///< REG_8 as unsigned byte
     int8_t         i8Byte;           ///< REG_8 as signed byte
     struct
     {
        unsigned b0:1;                ///< Bit 0 of REG_8 type
        unsigned b1:1;                ///< Bit 1 of REG_8 type
        unsigned b2:1;                ///< Bit 2 of REG_8 type
        unsigned b3:1;                ///< Bit 3 of REG_8 type
        unsigned b4:1;                ///< Bit 4 of REG_8 type
        unsigned b5:1;                ///< Bit 5 of REG_8 type
        unsigned b6:1;                ///< Bit 6 of REG_8 type
        unsigned b7:1;                ///< Bit 7 of REG_8 type
     };
  } REG_8;
a.b2
的值设为1,因此不存在对齐问题

删除
struct
会使
sizeof
的值为1,因此看起来似乎存在填充问题,但如果是,原因是什么

有人能解释一下吗?我正在使用Microchip XC16编译器(基于GCC),目标是一个16位处理器。

在您的计算机上可能是sizeof(unsigned)=2,因此任何“unsigned”位字段都至少占用2个字节。将unsigned替换为uint8应使sizeof(REG_8)变为1

另见这个问题:
看来@twin的想法是正确的,尽管我也找到了另一个解决方案。给出预期
sizeof(REG_8)==1
的两个备选方案是:

  typedef union
  {
     uint8_t        u8Byte;           ///< REG_8 as unsigned byte
     int8_t         i8Byte;           ///< REG_8 as signed byte
     struct
     {
        unsigned b0:1;                ///< Bit 0 of REG_8 type
        unsigned b1:1;                ///< Bit 1 of REG_8 type
        unsigned b2:1;                ///< Bit 2 of REG_8 type
        unsigned b3:1;                ///< Bit 3 of REG_8 type
        unsigned b4:1;                ///< Bit 4 of REG_8 type
        unsigned b5:1;                ///< Bit 5 of REG_8 type
        unsigned b6:1;                ///< Bit 6 of REG_8 type
        unsigned b7:1;                ///< Bit 7 of REG_8 type
     } __attribute__((packed));
  } REG_8;
typedef联合
{
uint8\u t u8Byte;//
……或者

  typedef union
  {
     uint8_t        u8Byte;           ///< REG_8 as unsigned byte
     int8_t         i8Byte;           ///< REG_8 as signed byte
     struct
     {
        uint8_t b0:1;                ///< Bit 0 of REG_8 type
        uint8_t b1:1;                ///< Bit 1 of REG_8 type
        uint8_t b2:1;                ///< Bit 2 of REG_8 type
        uint8_t b3:1;                ///< Bit 3 of REG_8 type
        uint8_t b4:1;                ///< Bit 4 of REG_8 type
        uint8_t b5:1;                ///< Bit 5 of REG_8 type
        uint8_t b6:1;                ///< Bit 6 of REG_8 type
        uint8_t b7:1;                ///< Bit 7 of REG_8 type
     };
  } REG_8;
typedef联合
{
uint8\u t u8Byte;//
我猜,由于严格的数据对齐寻址,16位机器的单字节联合将有1字节的填充。这是一个猜测,因为我不熟悉机器/编译器。@I在这台16位机器上,字确实需要在偶数地址上对齐,但机器可以在任何地址(即,无对齐要求)访问字节。sizeof(unsigned)为2,但:1限定符不是覆盖了结构成员的字节吗?此外,如果b7成员得到2个字节,那么union不应该增长到3个字节(2+7/8四舍五入)?结构成员将各自占用1位无符号字节。(我不理解你评论的第二部分-你将如何声明b7?)如果你声明未签名的b7:16,结构需要两个未签名的b7:16,因此我应该有大小为4Silliness的部分-我假设未签名的b7的LSB将代表它的值。请忽略!
  typedef union
  {
     uint8_t        u8Byte;           ///< REG_8 as unsigned byte
     int8_t         i8Byte;           ///< REG_8 as signed byte
     struct
     {
        uint8_t b0:1;                ///< Bit 0 of REG_8 type
        uint8_t b1:1;                ///< Bit 1 of REG_8 type
        uint8_t b2:1;                ///< Bit 2 of REG_8 type
        uint8_t b3:1;                ///< Bit 3 of REG_8 type
        uint8_t b4:1;                ///< Bit 4 of REG_8 type
        uint8_t b5:1;                ///< Bit 5 of REG_8 type
        uint8_t b6:1;                ///< Bit 6 of REG_8 type
        uint8_t b7:1;                ///< Bit 7 of REG_8 type
     };
  } REG_8;