tags:

views:

1551

answers:

5

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?

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

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

What are the general limits?

+1  A: 

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.

DrPizza
What are the general limits?
avd
There are no "general limits". On Windows, with default VC++ linker options and default CreateThread behaviour, typically something around 1 MiB per thread. On Linux, with an unlimited user, I believe that there is typically no limit (the stack can just grow downwards to occupy almost the entire address space).Basically, if you have to ask, you shouldn't be using the stack.
DrPizza
On embedded systems, you might have 4k or less. In which case you do have to ask even when it's reasonable to be using the stack. The answer is usually a Gallic shrug.
Steve Jessop
Ah true, also often the case in kernel mode.
DrPizza
+3  A: 

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.

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 --stack option of ld to do that.

paxdiablo
What are the general limits?
avd
@lex: there are no general limits. It depends on a lot of parameters.
Michael Foukarakis
@paxdiablo: WHat is meaning of reserved and commited?
avd
Reserved is how much address space to allocate, committed is how much to attach backing storage to. In other words, reserving address space does not mean the memory will be there when you need it. If you never use more than 4K stack, you're not wasting real memory for the other 1.6M. If you want to guarantee there'll be enough stack, reserved and committed should be identical.
paxdiablo
+2  A: 

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.

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())
Andreas Brinck
Thnk u very much
avd
And just for reference, a BFS is the same except that you use a FIFO instead of a stack.
Steve Jessop
Yes, or in STL-lingo use a std::deque with pop_front/push_back
Andreas Brinck
+1  A: 

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
pixelbeat
+1  A: 

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.

Dave Kirby