为什么堆栈地址朝着减少的内存地址增长? [英] Why does the stack address grow towards decreasing memory addresses?

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

问题描述

我在教科书中读到,堆栈是通过减少内存地址来增加的;即从高位地址到低位地址.这可能是一个不好的问题,但是我没有正确地理解这个概念.你能解释一下吗?

I read in text books that the stack grows by decreasing memory address; that is, from higher address to lower address. It may be a bad question, but I didn't get the concept right. Can you explain?

推荐答案

首先,它取决于平台.在某些体系结构中,堆栈是从地址空间的底部分配的,并且向上增长.

First, it's platform dependent. In some architectures, stack is allocated from the bottom of the address space and grows upwards.

假设像x86这样的体系结构从地址空间的顶部向下堆叠,那么这个想法很简单:

Assuming an architecture like x86 that stack grown downwards from the top of address space, the idea is pretty simple:

===============     Highest Address (e.g. 0xFFFF)
|             |
|    STACK    |
|             |
|-------------|  <- Stack Pointer   (e.g. 0xEEEE)
|             |
.     ...     .
|             |
|-------------|  <- Heap Pointer    (e.g. 0x2222)
|             |
|    HEAP     |
|             |
===============     Lowest Address  (e.g. 0x0000)

要增加堆栈,您需要减少堆栈指针:

To grow stack, you'd decrease the stack pointer:

===============     Highest Address (e.g. 0xFFFF)
|             |
|    STACK    |
|             |
|.............|  <- Old Stack Pointer (e.g. 0xEEEE)
|             |
| Newly       |
| allocated   |
|-------------|  <- New Stack Pointer (e.g. 0xAAAA)
.     ...     .
|             |
|-------------|  <- Heap Pointer      (e.g. 0x2222)
|             |
|    HEAP     |
|             |
===============     Lowest Address    (e.g. 0x0000)

如您所见,要增加堆栈,我们已经将堆栈指针从0xEEEE减小到0xAAAA,而要增加堆,则必须增加堆指针.

As you can see, to grow stack, we have decreased the stack pointer from 0xEEEE to 0xAAAA, whereas to grow heap, you have to increase the heap pointer.

显然,这是内存布局的简化.实际的可执行文件,数据段,...也被加载到内存中.此外,线程具有自己的堆栈空间.

您可能会问,为什么堆栈应该向下生长.好吧,正如我之前说的,有些架构则相反,使堆向下增长,而堆栈向上增长.将堆栈放在相对的两侧是很有意义的,因为它可以防止重叠,并且只要您有足够的可用地址空间,就可以使两个区域自由增长.

You may ask, why should stack grow downwards. Well, as I said before, some architectures do the reverse, making heap grow downwards and stack grow upwards. It makes sense to put stack and heap on opposite sides as it prevents overlap and allows both areas to grow freely as long as you have enough address space available.

另一个有效的问题可能是:程序是否应该减少/增加堆栈指针本身?架构如何向程序员强加一个?为什么它不那么依赖程序,而是依赖体系结构? 尽管您几乎可以与体系结构抗争并以某种相反的方式摆脱堆栈,但是某些直接修改堆栈指针的指令,尤其是callret,却会采用另一个方向,使情况变得一团糟.

Another valid question could be: Isn't the program supposed to decrease/increase the stack pointer itself? How can an architecture impose one over the other to the programmer? Why it's not so program dependent as it's architecture dependent? While you can pretty much fight the architecture and somehow get away your stack in the opposite direction, some instructions, notably call and ret that modify the stack pointer directly are going to assume another direction, making a mess.

这篇关于为什么堆栈地址朝着减少的内存地址增长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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