为什么堆栈大小有限制? [英] Why is there a limit on the stack size?

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

问题描述


可能重复:

什么是堆栈和堆

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

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?

推荐答案

实际上,堆栈确实 越来越多。通常情况下,它不需要很大,因此不需要很大。

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.

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

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.

这篇关于为什么堆栈大小有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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