代码之家  ›  专栏  ›  技术社区  ›  Mike

如何对齐结构的元素?

  •  1
  • Mike  · 技术社区  · 16 年前

    有没有办法使用GNUism强制对齐结构的特定元素?

    4 回复  |  直到 16 年前
        1
  •  3
  •   Cody Gray Felix    9 年前

    尝试 6.54.8 Structure-Packing Pragmas

    与Microsoft兼容 改变 构件的最大对齐度 结构(零宽度除外 位域)、联合和类 下面总是要求一个小的 二的幂并指定新的 对齐(字节)。

    1. #pragma pack(n)
    2. #pragma pack() 将对齐方式设置为 编译开始时的效果(请参见 还有命令行选项 -fpack-struct[=<n>] 请参见代码生成选项)。
    3. #pragma pack(push[,n]) 内部堆栈,然后可选
    4. #pragma pack(pop) 将对齐设置恢复为在保存时的设置 删除该堆栈项)。请注意 #pragma pack([n]) 不影响内部堆栈;就是这样 可能有 #pragma pack(push) 然后是多个 实例并由单个

    支持 ms_struct #pragma 哪一个 按文档所示的方式布置结构 __attribute__ ((ms_struct)) .

    1. #pragma ms_struct on
    2. #pragma ms_struct off 关闭已声明结构的布局。
    3. #pragma ms_struct reset 返回默认布局。
        2
  •  3
  •   caskey    16 年前

    http://gcc.gnu.org/onlinedocs/gcc-3.1.1/gcc/Type-Attributes.html

    aligned (alignment)
    This attribute specifies a minimum alignment (in bytes) for variables of the specified type. > For example, the declarations:
            struct S { short f[3]; } __attribute__ ((aligned (8)));
            typedef int more_aligned_int __attribute__ ((aligned (8)));
    
        3
  •  0
  •   Phong    16 年前

    __attribute__((__aligned__(8)))
    
        4
  •  0
  •   nategoose    16 年前

    除了列出的东西,你可能还对 属性