Ç64位指针对准 [英] C 64-bit Pointer Alignment

查看:202
本文介绍了Ç64位指针对准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在64位系统上的指针仍然4字节对齐(类似于双在32位系统)?或者,他们注意到8字节对齐?

Are pointers on a 64-bit system still 4 byte aligned (similar to a double on a 32 bit system)? Or are they note 8 byte aligned?

例如,在64位系统有多大下面的数据结构:

For example, on a 64-bit system how big is the following data structure:

struct a {
    void* ptr;
    char myChar;
}

请问指针8字节对齐,导致7个字节填充的字符(总= 8 + 8 = 16)?或者将鼠标指针是4字节对齐(4字节+ 4个字节),造成3个字节的填充(总= 4 + 4 + 4 = 12)?

Would the pointer by 8 byte aligned, causing 7 bytes of padding for the character (total = 8 + 8 = 16)? Or would the pointer be 4 byte aligned (4 bytes + 4 bytes) causing 3 bytes of padding (total = 4 + 4 + 4 = 12)?

谢谢, 瑞安

推荐答案

数据对齐和包装的具体实施中,可以从编译器设置被更改,通常(或甚至编译指示)。

Data alignment and packing are implementation specific, and can be usually changed from compiler settings (or even with pragmas).

但是假设你使用默认设置,在大多数(如果不是全部)编译器的结构应该最终会被16字节的总和。其原因是因为计算机读取数据块具有其天然字的大小(这是在64位的系统的8个字节)的大小。如果它是垫它到4字节偏移,下一个结​​构不会被正确填充到64位边界。例如在一情况下,ARR [2],则阵列的第二个元素将开始在12字节的偏移量,这是不是在机器的本机字节边界

However assuming you're using default settings, on most (if not all) compilers the structure should end up being 16 bytes total. The reason is because computers reads a data chunk with size of its native word size (which is 8 bytes in 64-bit system). If it were to pad it to 4 byte offsets, the next structure would not be properly padded to 64-bit boundary. For example in case of a arr[2], the second element of the array would start at 12-byte offset, which isn't at the native byte boundary of the machine.

这篇关于Ç64位指针对准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆