沿着4字节边界对齐边界、字节

2023-09-11 07:33:14 作者:失心瘋的男人

最近我在思考对齐......这件事情,我们通常不具备的考虑,但我已经意识到有些处理器要求的对象以及4字节边界对齐。究竟这是否意味着,和哪些特定的系统有对齐要求?

I recently got thinking about alignment... It's something that we don't ordinarily have to consider, but I've realized that some processors require objects to be aligned along 4-byte boundaries. What exactly does this mean, and which specific systems have alignment requirements?

假设我有一个任意的指针:

Suppose I have an arbitrary pointer:

无符号字符* PTR

现在,我尝试从一个存储单元中的双重价值:

Now, I'm trying to retrieve a double value from a memory location:

双D = **((双*)PTR);

难道这会导致问题?

推荐答案

这肯定会产生一些系统上的问题。

It can definitely cause problems on some systems.

例如,在基于ARM的系统不能处理未对齐一个4字节边界的32位的字。这样做会导致访问冲突异常。在x86上,您可以访问这种非对齐的数据,尽管性能会受到影响,因为两个词一点都取从内存中,而不是只有一个。

For example, on ARM-based systems you cannot address a 32-bit word that is not aligned to a 4-byte boundary. Doing so will result in an access violation exception. On x86 you can access such non-aligned data, though the performance suffers a little since two words have to fetched from memory instead of just one.

 
精彩推荐
图片推荐