过度使用 Pragma Pack(1)
Overuse of Pragma Pack(1)
过度使用 #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 takes effect at the first struct, union, or class declaration after the pragma is seen."
过度使用 #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 takes effect at the first struct, union, or class declaration after the pragma is seen."