C/C++ 程序的最大堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:17:46  来源:igfitidea点击:

C/C++ maximum stack size of program

c++cstack

提问by avd

I want to do DFS on a 100 X 100 array. (Say elements of array represents graph nodes) So assuming worst case, depth of recursive function calls can go upto 10000 with each call taking upto say 20 bytes. So is it feasible means is there a possibility of stackoverflow?

我想在 100 X 100 阵列上执行 DFS。(假设数组的元素代表图节点)所以假设最坏的情况,递归函数调用的深度可以达到 10000,每次调用最多占用 20 个字节。那么它是否可行意味着是否有可能出现stackoverflow?

What is the maximum size of stack in C/C++?

C/C++ 中堆栈的最大大小是多少?

Please specify for gcc for both
1) cygwin on Windows
2) Unix

请为 gcc 指定
1) Windows 上的 cygwin
2) Unix

What are the general limits?

一般限制是什么?

采纳答案by Andreas Brinck

In Visual Studio the default stack size is 1 MB i think, so with a recursion depth of 10,000 each stack frame can be at most ~100 bytes which should be sufficient for a DFS algorithm.

在 Visual Studio 中,我认为默认堆栈大小为 1 MB,因此每个堆栈帧的递归深度为 10,000,最多可以为 ~100 字节,这对于 DFS 算法来说应该足够了。

Most compilers including Visual Studio let you specify the stack size. On some (all?) linux flavours the stack size isn't part of the executable but an environment variable in the OS. You can then check the stack size with ulimit -sand set it to a new value with for example ulimit -s 16384.

大多数编译器(包括 Visual Studio)允许您指定堆栈大小。在某些(所有?)linux 风格中,堆栈大小不是可执行文件的一部分,而是操作系统中的环境变量。然后,您可以使用例如检查堆栈大小ulimit -s并将其设置为新值ulimit -s 16384

Here's a linkwith default stack sizes for gcc.

这是gcc 默认堆栈大小的链接

DFS without recursion:

没有递归的DFS:

std::stack<Node> dfs;
dfs.push(start);
do {
    Node top = dfs.top();
    if (top is what we are looking for) {
       break;
    }
    dfs.pop();
    for (outgoing nodes from top) {
        dfs.push(outgoing node);
    }
} while (!dfs.empty())

回答by pixelbeat

stacks for threads are often smaller. You can change the default at link time, or change at run time also. For reference some defaults are:

线程的堆栈通常较小。您可以在链接时更改默认值,也可以在运行时更改。作为参考,一些默认值是:

  • glibc i386, x86_64 7.4 MB
  • Tru64 5.1 5.2 MB
  • Cygwin 1.8 MB
  • Solaris 7..10 1 MB
  • MacOS X 10.5 460 KB
  • AIX 5 98 KB
  • OpenBSD 4.0 64 KB
  • HP-UX 11 16 KB
  • glibc i386、x86_64 7.4 MB
  • Tru64 5.1 5.2 MB
  • Cygwin 1.8 MB
  • Solaris 7..10 1 MB
  • MacOS X 10.5 460 KB
  • AIX 5 98 KB
  • OpenBSD 4.0 64 KB
  • HP-UX 11 16 KB

回答by DrPizza

Platform-dependent, toolchain-dependent, ulimit-dependent, parameter-dependent.... It is not at all specified, and there are many static and dynamic properties that can influence it.

平台相关、工具链相关、ulimit 相关、参数相关.... 完全没有指定,并且有许多静态和动态属性可以影响它。

回答by paxdiablo

Yes, there is a possibility of stack overflow. The C and C++ standard do not dictate things like stack depth, those are generally an environmental issue.

是的,存在堆栈溢出的可能性。C 和 C++ 标准没有规定堆栈深度之类的东西,这些通常是环境问题。

Most decent development environments and/or operating systems will let you tailor the stack size of a process, either at link or load time.

大多数体面的开发环境和/或操作系统都可以让您在链接或加载时定制进程的堆栈大小。

You should specify which OS and development environment you're using for more targeted assistance.

您应该指定您使用的操作系统和开发环境以获得更有针对性的帮助。

For example, under Ubuntu Karmic Koala, the default for gcc is 2M reserved and 4K committed but this can be changed when you link the program. Use the --stackoption of ldto do that.

例如,在 Ubuntu Karmic Koala 下,gcc 的默认值是 2M 保留和 4K 已提交,但这可以在您链接程序时更改。使用--stack选项ld来做到这一点。

回答by Owl

I just ran out of stack at work, it was a database and it was running some threads, basically the previous developer had thrown a big array on the stack, and the stack was low anyway. The software was compiled using Microsoft Visual Studio 2015.

刚工作的时候栈用完了,是一个数据库,运行了一些线程,基本上之前的开发者在栈上扔了一个大数组,反正栈是低的。该软件是使用 Microsoft Visual Studio 2015 编译的。

Even though the thread had run out of stack, it silently failed and continued on, it only stack overflowed when it came to access the contents of the data on the stack.

即使线程已用完堆栈,它也默默地失败并继续运行,只有在访问堆栈上的数据内容时才会堆栈溢出。

The best advice i can give is to not declare arrays on the stack - especially in complex applications and particularly in threads, instead use heap. That's what it's there for ;)

我能给出的最好建议是不要在堆栈上声明数组——尤其是在复杂的应用程序中,尤其是在线程中,而是使用堆。这就是它的用途;)

Also just keep in mind it may not fail immediately when declaring the stack, but only on access. My guess is that the compiler declares stack under windows "optimistically", i.e. it will assume that the stack has been declared and is sufficiently sized until it comes to use it and then finds out that the stack isn't there.

另外请记住,在声明堆栈时它可能不会立即失败,而只会在访问时失败。我的猜测是编译器在 windows 下“乐观地”声明堆栈,即它会假设堆栈已经被声明并且足够大,直到它使用它然后发现堆栈不存在。

Different operating systems may have different stack declaration policies. Please leave a comment if you know what these policies are.

不同的操作系统可能有不同的堆栈声明策略。如果您知道这些政策是什么,请发表评论。

回答by Dave Kirby

I am not sure what you mean by doing a depth first search on a rectangular array, but I assume you know what you are doing.

我不确定对矩形数组进行深度优先搜索是什么意思,但我假设您知道自己在做什么。

If the stack limit is a problem you should be able to convert your recursive solution into an iterative solution that pushes intermediate values onto a stack which is allocated from the heap.

如果堆栈限制是一个问题,您应该能够将递归解决方案转换为迭代解决方案,将中间值推送到从堆分配的堆栈上。