What causes padding due to overuse of #pragma pack(1)
? What actually happens in backstage?
#pragma pack(1)
typedef struct
{
union
{
uint8_t SAM;
#pragma pack(1)
struct {
uint8_t sm:1;
uint8_t sm1:3 ;
uint8_t sm2 :1 ;
uint8_t sm3 :1 ;
uint8_t sm4 :1 ;
uint8_t sm5:1 ;
};
#pragma pack()
};
}SAM1;
#pragma pack()
Your annotated code:
#pragma pack(1)
// from here on the compiler will align fields on 1 byte
typedef struct
{
union
{
uint8_t SAM;
#pragma pack(1)
// there is no use for this pragma on a bit field
// packing was already at 1 byte
struct {
uint8_t sm:1;
uint8_t sm1:3 ;
uint8_t sm2 :1 ;
uint8_t sm3 :1 ;
uint8_t sm4 :1 ;
uint8_t sm5:1 ;
};
#pragma pack()
// from the next struct or union declaration the compiler will use default packing
};
}SAM1;
#pragma pack()
// from here on the compiler uses default packing
"Pack takes effect at the first struct, union, or class declaration after the pragma is seen."