堆栈可以长成堆吗? [英] Can the stack grow into the heap?

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

问题描述

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

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.

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

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