是什么导致由于#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()
最佳答案
您带注释的代码:
#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在第一个结构,联合或类声明处生效。”
关于c++ - 过度使用Pragma Pack(1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62424749/