Struct Alignment Specify
在上一篇《Struct Alignment Rule》中,我们梳理了结构体存储布局的“地址边界对齐限制”规则。
本篇介绍通过编译器 gcc/msvc 提供的扩展特性及 C/C++ 提供的一些语言特性来修改默认的对齐参数,并测试分析其作用效果。
- The
packed
attribute specifies that a structure member should have the smallest possible alignment. - The
aligned
attribute specifies a minimum alignment for the variable or structure field, measured in bytes. -fpack-struct[=n]
/#pragma pack(n)
specifies the maximum alignment, structure members can potentially be unaligned.