为什么需要内存对齐? [英] Why is memory alignment required?

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

问题描述

可能重复:
目的是进行内存对齐

Possible Duplicate:
Purpose of memory alignment

我在网络上阅读了一些有关内存对齐的文章,并且可以理解,通过正确对齐的内存(采用2字节对齐),我们可以一次性快速获取数据.

I read some articles on net about memory alignment and could understand that from properly aligned memory (take 2-byte alignment) we can fetch data fastly in one go.

但是,如果我们像单个硬件一样拥有内存,那么给定一个地址,为什么我们不能直接从该位置读取2字节.例如:

But if we have memory like a single hardware piece, then given an address, why cannot we read 2-byte directly from that position. like:

我考虑了一下.我认为,如果记忆是在奇偶库中,那么该理论将适用.

I thought over it. I think that if the memory is in odd-even banks kind of then the theory would apply.

我想念什么?

推荐答案

您的图片描述了我们(人类)如何可视化计算机内存.

Your pictures describe how we (humans) visualize computer memory.

实际上,将内存视为巨大的位矩阵. 每个矩阵列都有一个读取器",可以读取/写入该列的任何位. 每个矩阵行都有一个选择器",可以选择读取器将要读取/写入的特定位.

In reality, think about memory as huge matrix of bits. Each matrix column has a "reader" attached that can read/write any bit from this column. Each matrix row has a "selector", which can select the specific bit that the reader will read/write.

因此,该阅读器可以一次读取整个选定的矩阵行. 该行的长度(矩阵列数)定义了一次可以读取多少数据. 例如,如果您有64列,则您的内存控制器可以一次读取8个字节(不过通常可以做得更多).

Therefore, this reader can read the whole selected matrix row at once. Length of this row (number of matrix columns) define how much data can be read at once. For instance, if you have 64 columns, your memory controller can read 8 bytes at once (it usually can do more than that though).

只要保持数据对齐,您将需要较少的这些内存访问. 即使您只需要读取两位,但是它们位于不同的行上,您也将需要两次对内存的访问,而不是一次.

As long as you keep your data aligned, you will need less of these memory accesses. Even if you need to read just two bits, but they are located on different rows, you will need two accesses to memory instead of one.

此外,还有写作的整个方面,这是一个不同的问题.

Also, there's a whole aspect of writing, which is a different problem.

就像您可以读取整行一样,您也可以编写整行. 如果数据不对齐,那么当您写的内容不是完整的行时,您将需要执行read-modify-write(读取行的旧内容,修改相关部分并写入新内容).

Just as you can read the whole row, you also can write the whole row. If your data isn't aligned, when you write something that is not a full row, you will need to do read-modify-write (read the old content of the row, modify the relevant part and write the new content).

这篇关于为什么需要内存对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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