本文介绍了栈可以长成堆吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习操作系统,我了解到堆栈位于内核和堆之间.令我困惑的是,在大多数实现中,由于堆栈倾向于向下增长而堆增长到更高的内存地址,是什么阻止了堆栈增长到堆中?如果有可能,如果它确实增长到堆中会发生什么?

I'm currently learning about operating systems and I learned that the stack is located between the kernel and the heap. What confuses me is that in most implementations, since the stack tends to grow downwards while the heap grows to higher memory addresses what is stopping the stack from growing into the heap? If it were possible what would happen if it did grow into the heap?

推荐答案

传统上非常简化的内存视图看起来像这样:

The much-simplified view of memory traditionally looks something like this:

 ===================
| Operating System  | High memory
 ===================
|   Your program    |
|  ---------------  |
| | Process stack | |
|  ---------------  | Transient program area
| |  Process heap | |
|  ---------------  |
| |  Program code | |
|  ---------------  |
 ===================
| Operating system  | Low memory
 ===================

正如您所指出的,进程堆栈从操作系统代码下方开始,然后向下增长.另一方面,进程堆从固定程序代码的上方开始,然后向上增长.

As you pointed out, the process stack starts just below the operating system code, and grows downward. The process heap, on the other hand, starts just above the fixed program code, and grows upward.

在早期的 PC 操作系统中,这实际上是内存中程序的物理布局.例如,CP/M 操作系统为某些操作系统引导代码保留了前 256 个字节的内存,其余必要的操作系统服务占用了高内存区域.程序从地址 0x0100 开始,并且可以使用从那里到顶部操作系统代码开始之间的所有内存.MS-DOS 非常相似.

In the early days of PC operating systems that really was the physical layout of a program in memory. The CP/M operating system, for example, reserved the first 256 bytes of memory for some operating system bootstrap code, and the rest of the necessary operating system services occupied the high memory area. Programs started at address 0x0100, and could use all the memory between there and the start of the operating system code at the top. MS-DOS was very similar.

没有任何防护措施可以防止您提到的事情发生:程序会在堆栈上分配如此多的空间,以至于它会覆盖在堆上分配的内存.或者,程序将分配覆盖处理器堆栈的堆内存.当这些事情发生时,程序就会崩溃.在某些情况下,操作系统也会崩溃.当然,因为一次只能运行一个程序,这没什么大不了的:只需重新启动机器并重试即可.

There were no guardrails to prevent what you mentioned from happening: a program would allocate so much space on the stack that it overwrote memory that was allocated on the heap. Or, the program would allocate heap memory that overwrote the processor stack. When either of those things happened, the program would crash. In some cases, the operating system would crash, too. Of course, because only one program could be running at a time, that wasn't a huge deal: just reboot the machine and try again.

现代计算机具有更先进的内存布局,而这种概念图不再适用.今天的操作系统可以更好地执行内存访问限制.例如,一个进程为其堆栈分配了一个固定段(通常大约为 1 兆字节).如果程序试图使用比分配更多的堆栈空间,内存管理器将不允许它.该程序将因访问冲突而崩溃.并且程序的堆不能增长到分配给栈的内存中也是出于同样的原因.

Modern computers have much more advanced memory layouts, and that conceptual picture no longer holds true. Operating systems today can do a much better job of enforcing memory access restrictions. A process, for example, is assigned a fixed segment for its stack (typically on the order of 1 megabyte). If the program tries to use more stack space than is allocated, the memory manager won't allow it. The program will crash with an access violation. And the program's heap can't grow into the memory allocated to the stack for the same reason.

这篇关于栈可以长成堆吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:11