c++ - 过度使用Pragma Pack(1)

标签 c++ c gnu

是什么导致由于#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/

相关文章:

c - 如何在c程序中使用分段?

Linux Makefile - CXX 未定义

opencv - 如何在 Windows 上安装 NAWK?

c++ - sqlite c++ 和 unsigned int key

c - GNU 内联汇编中的百分比 (%) 符号(C 中的 asm)

c++ - 如何修复 QHBoxLayout 项目大小并向 QHBoxLayout 中的每个项目添加下拉列表

c - 递归没有得到我需要的输出

linux - 找不到 gcc-arm-linux-gnueabi 命令

c++ - 将枚举分配给变量时出错

c++ - 运算符 < 和 > 如何与指针一起使用?