由于过度使用#pragma pack(1)而导致填充的原因是什么?后台到底发生了什么?
#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()发布于 2020-06-17 08:48:57
您的注释代码:
#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“在看到语用之后,包在第一个结构、联合或类声明中生效。”
https://stackoverflow.com/questions/62424749
复制相似问题