为什么堆栈大小有限制?

发布于 2024-11-19 04:45:22 字数 320 浏览 9 评论 0原文

可能的重复:
堆栈和堆是什么以及在哪里

我的 Ubuntu 安装默认堆栈大小限制为 8 MB。但我很好奇为什么我们需要限制用户程序的堆栈大小。同一个程序可以通过 malloc/mmap 等使用其所有 4 GB(对于 32 位程序)可寻址空间。那么为什么我们需要堆栈大小限制呢?为什么堆栈不能增长直到几乎与堆相交?

Possible Duplicate:
What and where are the stack and heap

My installation of Ubuntu has a default stack size limit of 8 MB. But I am curious as to why we need to restrict a user program's stack size. The same program can use all of its 4 GB (for a 32 bit program) addressable space via malloc/mmap etc. So why do we need a stack size limit? Why can't the stack grow till it almost meets the heap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-11-26 04:45:22

事实上,堆栈确实增长得越来越大。它不需要一开始就很大,因为在一般情况下,它不需要很大。它太大会导致内存占用的浪费。

我不能 100% 确定堆栈在 Linux 上是如何实现的,但在 Windows 上,为堆栈保留了大量空间。这个数量可以在编译器选项中设置(对于深度递归程序,您可能需要更大的堆栈)。在运行时,堆栈可以通过保护页系统动态扩展。在堆栈的末尾有一个保护页,当它被击中时,堆栈将扩展一个额外的页面,并将保护页向前推一页。

堆栈探测是另一个有趣且相关的概念。所以你的问题是“为什么堆栈不能增长直到它几乎与堆相遇?”堆栈确实会增长,但由于大多数时候拥有巨大的堆栈可能是错误的不良副作用,因此保留的大小不会很大(尽管这是可设置的)。

这篇文章非常有趣并且与您的问题相关。

In fact the stack does grow more and more. It doesn't need to start very big since in the general case, it doesn't need to be very big. Having it as very big results in a wasteful memory footprint.

I'm not 100% sure as to how the stack is implemented on Linux but on Windows, a large amount of space is reserved for the stack. This amount can be set in compiler options (you may want a larger stack for deeply recursive programs). At runtime, the stack can be extended dynamically via a guard page system. At the end of the stack there is a guard page which when hit will extend the stack by an extra page and push the guard page forward by one.

Stack probing is another interesting and related concept. So your question of 'why can't the stack grow till it almost meets the heap?' The stack does grow but since most of the time having a huge stack is likely an undesired side-effect of a bug, the reserved size will not be huge (although this is settable).

This article is very interesting and relevant to your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文