C/C++ maximum stack size of program
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.
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
.
Here's a link with default stack sizes for gcc.
DFS without recursion:
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())
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