什么是“堆叠对齐”? [英] what is "stack alignment"?

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

问题描述

什么是堆栈对齐?
为什么使用它?
可以通过编译器设置控制吗?

What is stack alignment? Why is it used? Can it be controlled by compiler settings?

这个问题的细节是从一个问题,当试图使用ffmpeg库与msvc,但是我'

The details of this question are taken from a problem faced when trying to use ffmpeg libraries with msvc, however what I'm really interested in is an explanation of what is "stack alignment".

详细信息:


  • 当runnig我的msvc编译程序链接到avcodec我得到
    以下错误:编译器没有对齐堆栈变量Libavcodec有
    被错误编译,其次是avcodec崩溃.dll。

  • avcodec.dll没有使用msvc编译,因此我无法看到里面正在发生什么。

  • 运行ffmpeg.exe和使用相同的avcodec.dll一切都很好。

  • ffmpeg.exe未使用msvc编译,符合gcc / mingw(与avcodec.dll相同)

  • When runnig my msvc complied program which links to avcodec I get the following error: "Compiler did not align stack variables. Libavcodec has been miscompiled", followed by a crash in avcodec.dll.
  • avcodec.dll was not compiled with msvc, so I'm unable to see what is going on inside.
  • When running ffmpeg.exe and using the same avcodec.dll everything works well.
  • ffmpeg.exe was not compiled with msvc, it was complied with gcc / mingw (same as avcodec.dll)

感谢,

Dan

推荐答案

在过去的计算机中有一个8位的数据总线。这意味着,每个时钟周期可以处理8位信息。这是很好的。

In the past computers had an 8 bits databus. This means, that each clock cycle 8 bits of information could be processed. Which was fine then.

然后16位电脑。由于向下兼容性和其他问题,保留了8位字节,并引入了16位字。每个字是2字节。并且每个时钟周期可以处理16位信息。但这给了一个小问题。

Then came 16 bit computers. Due to downward compatibility and other issues, the 8 bit byte was kept and the 16 bit word was introduced. Each word was 2 bytes. And each clock cycle 16 bits of information could be processed. But this gave a small problem.

让我们看一个内存映射:

Let's look at a memory map:

+----+
|0000| 
|0001|
+----+
|0002|
|0003|
+----+
|0004|
|0005|
+----+
| .. |

每个地址都有一个字节。其中可以单独访问。
但是字只能在偶地址处提取。因此,如果我们在0000读取字,我们读取0000和0001处的字节。但是如果我们想读取位置0001处的字,则需要两次读访问。首先是0000,0001然后是0002,0003,我们只保留0001,0002。

At each address there is a byte. Which can be accessed individually. But words can only be fetched at even adresses. So if we read a word at 0000, we read the bytes at 0000 and 0001. But if we want to read the word at position 0001, we need two read accesses. First 0000,0001 and then 0002,0003 and we only keep 0001,0002.

当然,这需要一些额外的时间,这是不赞赏。所以这就是为什么他们发明了对齐。因此,我们将字变量存储在字边界处,并将字节变量存储在字节边界。

Of course this took some extra time and that was not appreciated. So that's why they invented alignment. So we store word variables at word boundaries and byte variables at byte boundaries.

例如,如果我们有一个带有字节字段W)(和一个非常天真的编译器,我们得到以下:

For example, if we have a structure with a byte field (B) and a word field (W) (and a very naive compiler, we get the following:

+----+
|0000| B
|0001| W
+----+
|0002| W
|0003|
+----+

这不是乐趣,但是当使用字对齐时,我们可以找到:

Which is not fun. But when using word alignment we find:

+----+
|0000| B
|0001| -
+----+
|0002| W
|0003| W
+----+

你可以想象,当使用双字(4字节)或四字(8字节)时,这是更重要的,这就是为什么现代编译器,您可以选择在编译程序时使用的对齐方式。

You can imagine that when using double word (4 bytes) or quad word (8 bytes) this is even more important. That's why with most modern compilers you can chose which alignment you are using while compiling the program.

这篇关于什么是“堆叠对齐”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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