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

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

问题描述

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

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天全站免登陆