为什么 C 中的 double 是 8 个字节对齐的? [英] Why double in C is 8 bytes aligned?

查看:25
本文介绍了为什么 C 中的 double 是 8 个字节对齐的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一篇关于内存中数据类型对齐的文章(此处)理解一点即

I was reading a article about data types alignment in memory(here) and I am unable to understand one point i.e.

注意双变量将在 32 上的 8 字节边界上分配位机,需要两个内存读取周期.在 64 位机器上,根据银行的数量,双变量将被分配到 8 字节边界,只需要一个内存读取周期.

Note that a double variable will be allocated on 8 byte boundary on 32 bit machine and requires two memory read cycles. On a 64 bit machine, based on number of banks, double variable will be allocated on 8 byte boundary and requires only one memory read cycle.

我的疑问是:为什么需要在 8 字节边界而不是 4 字节上分配双变量?如果它在 4 字节边界上分配,我们仍然只需要 2 个内存读取周期(在 32 位机器上).如果我错了,请纠正我.

My doubt is: Why double variables need to be allocated on 8 byte boundary and not on 4 byte? If it is allocated on 4 byte boundary still we need only 2 memory read cycles(on a 32 bit machine). Correct me if I am wrong.

另外,如果有人有关于成员/内存对齐的好教程,请分享.

Also if some one has a good tutorial on member/memory alignment, kindly share.

推荐答案

将大小为 2^N 的数据值对齐在 2^N 的边界上的原因是为了避免该值在缓存中拆分的可能性线边界.

The reason to align a data value of size 2^N on a boundary of 2^N is to avoid the possibility that the value will be split across a cache line boundary.

x86-32 处理器最多可以在两次 32 位内存读取中从任何字边界(8 字节对齐或不对齐)获取双精度值.但是,如果该值跨越缓存线边界拆分,那么获取第二个字的时间可能会很长,因为需要从内存中获取第二个缓存线.这会不必要地产生较差的处理器性能.(实际上,当前的处理器不会一次从内存中获取 32 位数据;它们倾向于在更宽的总线上获取更大的值以实现真正高的数据带宽;如果它们需要获取两个字的实际时间位于同一缓存行中,并且已经缓存,可能只有 1 个时钟).

The x86-32 processor can fetch a double from any word boundary (8 byte aligned or not) in at most two, 32-bit memory reads. But if the value is split across a cache line boundary, then the time to fetch the 2nd word may be quite long because of the need to fetch a 2nd cache line from memory. This produces poor processor performance unnecessarily. (As a practical matter, the current processors don't fetch 32-bits from the memory at a time; they tend to fetch much bigger values on much wider busses to enable really high data bandwidths; the actual time to fetch both words if they are in the same cache line, and already cached, may be just 1 clock).

这种对齐方案的一个免费结果是这些值也不会跨越页面边界.这避免了在数据获取过程中出现页面错误的可能性.

A free consequence of this alignment scheme is that such values also do not cross page boundaries. This avoids the possibility of a page fault in the middle of an data fetch.

因此,出于性能原因,您应该在 8 字节边界上对齐双打.编译器知道这一点,只会为你做.

So, you should align doubles on 8 byte boundaries for performance reasons. And the compilers know this and just do it for you.

这篇关于为什么 C 中的 double 是 8 个字节对齐的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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