C/C++ 程序的最大堆栈大小 [英] C/C++ maximum stack size of program

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

问题描述

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

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天全站免登陆