この形式の 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;
残念ながら、sizeof(REG_8)
予想される 1 ではなく 2 を返します。REG_16 と REG_32 の同様の定義は、予想どおり 2 と 4 のサイズを返します。sizeof(uint8_t)
期待どおり、1をsizeof(int8_t)
返します。
タイプは期待どおりに機能します。例えば、
REG_8 a;
a.u8Byte = 4;
は値 1を与えるa.b2
ため、アラインメントの問題はありません。
を削除するstruct
とsizeof
値が 1 になるため、パディングの問題があるように見えますが、もしそうなら、なぜですか?
誰でもこれを説明できますか?16 ビット プロセッサを対象とする Microchip XC16 コンパイラ (GCC ベース) を使用しています。