为什么iOS中的结构对齐方式不同
Why struct alignment different in iOS
我已经将Xcode中的结构对齐设置为8字节打包。例如:
struct Info
{
unsigned short kindID;
char kindName[32];
int64_t gameMonery;
}__attribute__ ((aligned(8)));
此结构体大小为48字节,在iPhone5和iPhone 5S中,kindName
地址偏移量为2字节,但在iPhone 5中,gameMoney
偏移量为36字节,在iPhone 5S中gameMoney
偏移量为40字节。
为什么会有这种不同?
在32位平台上,64位类型(int64_t
)的对齐为4,在64位平台上为8。
通过在声明中添加__attribute__((packed))
,可以使编译器抑制各个元素的对齐。未对齐成员的访问速度稍慢。
结构的大小在两个平台上没有差异的原因是大小取决于结构的对齐(指定为8):
示例:
struct { char i; } __attribute__((aligned(8))) foo;
sizeof(foo) == 8;
相关文章:
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 具有调整对齐方式的类型定义
- size_t的大小和对齐方式是否与ptrdiff_t相同?
- 逗号运算符在对齐方式中
- vcpkg:指定结构成员对齐方式
- 使用字节数组具有单字节对齐方式的结构是否安全
- C++对齐方式(何时使用对齐方式)
- 编译器会秘密增加结构的对齐方式吗?
- C++17 中函数参数的指针对齐方式
- 未在此范围内声明的对齐方式?
- 如何控制 clang 格式新行中 lessless 的对齐方式
- C++ 编译器正在更改我的结构的对齐方式.我怎样才能防止这种情况
- 是否有符合标准的方法来确定非静态杆件的对齐方式?
- 重载运算符是具有较小默认对齐方式的新增运算符
- 结构和对齐方式的大小
- Sizeof 舍入到对齐方式,但编译器仍将对象放在剩余的字节中
- 是否可以让 std::vector<char> 使用选定的内存对齐方式分配内存
- 对齐方式与指针中尾随零的数量有何关系
- 对齐方式究竟如何影响内存布局和新放置的巴哈维?
- 使用 auto 关键字时不遵守对齐方式 16