0

我正在尝试通过实现书籍中的一些内容来更多地了解虚拟机和编程语言。我目前正在阅读的这本书将堆栈和堆保存在一个内存区域中。栈向上增长,堆向下增长。我想知道除了加载/存储操作的更简单策略之外,这样做的好处是什么,因为您不需要区分两个不同的内存区域。

我问的原因是因为我正在考虑偏离书中的计划,并为堆栈和堆设置两个不同的内存区域。这对我来说似乎更有意义,我不必担心堆栈和堆寄存器会相互冲突。

4

2 回答 2

1

I'd like to know what the benefits of this are other than maybe a simpler strategy for load/store operations (...)

The benefit is that you don't need virtual memory, which makes this concept work on the simplest of CPUs/architectures. Also you don't need an operating system that keeps track of memory areas and their assignment to programs. In other words, such an implementation is well suited for e.g. small-scale embedded systems that typically don't have (and don't need) the processing power of a modern day desktop or server CPU.

(...) because you don't need to distinguish between two different memory areas.

I suppose by memory areas you mean the concept of multiple contigous memory spaces which are separated such that they each have an address range of relative indexes (0, ..., n) where n is the maximum number of bytes in that area.

That makes a lot of sense if you have virtual memory, that is the CPU implements a layer on top of the physical memory that gives programs the illusion of each having a seperate, contigous memory space.

于 2014-02-02T07:42:18.487 回答
1

好吧,在内存有限且没有虚拟内存的旧机器上,这两个内存段代表了机器可用空间的两端。如果你有 48k 的连续空间,一端是向上增长的堆栈,另一端是向下增长的堆。最终它们发生碰撞,您“内存不足”。

所以这就是灵感的来源。现代机器当然不会受到内存挑战(必然),并且您拥有 VM 系统来帮助隔离不同的部分,例如堆和堆栈。那么你就没有真正的理由在其中一个上“长大”而在另一个上“长大”。相反,您将它们分配到所需的大小,然后开始长大,直到用完为止。

此外,当然,今天,您可以使用 VM 控制堆栈(甚至堆区域)上的执行等。过去在更小、更旧的机器上并不是真正的问题。

于 2014-02-02T06:59:30.023 回答