方案C / C ++最大堆栈大小 [英] C/C++ maximum stack size of program

查看:247
本文介绍了方案C / C ++最大堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的DFS 100×100阵列上。 (说阵列重新presents图节点的元素)因此,假设最坏的情况下,递归函数调用的深度可以高达10000每次调用服用高达20说字节。因此,它是可行的办法是有计算器的可能性?

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?

什么是C / C ++栈的最大尺寸?

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

请指定GCC两种结果
  1)在Windows上的cygwin搜索
  2)的Unix

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

什么一般限制?

推荐答案

在Visual Studio中的默认堆栈大小为1 MB,我认为,所以用的10.000递归深度每个堆栈帧可以在它应该是最〜100个字节足够的DFS算法。

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中让你指定的堆栈大小。在某些(所有?)Linux版本的堆栈大小不是可执行文件,但在操作系统的环境变量的一部分。然后,您可以请与的ulimit -s 堆栈大小,并将其设置为与例如的ulimit -s 16384 。

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 -s and set it to a new value with for example ulimit -s 16384.

下面是使用默认堆栈大小为GCC一个链接

Here's a link with default stack sizes for gcc.

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())

这篇关于方案C / C ++最大堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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