Maximum C / C ++ Stack Size

I want to make DFS in a 100 X 100 array. (Let's say the elements of the array represent the nodes of the graph). Thus, assuming the worst case, the depth of recursive function calls can reach 10,000 for each call that takes up to 20 bytes. So it's possible, is there stackoverflow capability?

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

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

What are the common limitations?

+83
c ++ c stack
Dec 01 '09 at 12:42
source share
6 answers

In Visual Studio, the default stack size is 1 MB, I think, therefore, with a recursion depth of 10,000, each stack frame can be no more than ~ 100 bytes, which should be sufficient for the DFS algorithm.

Most compilers, including Visual Studio, allow you to specify the size of the stack. On some (all?) Linux flavors, the stack size is not part of the executable, but an environment variable in the OS. Then you can check the stack size with ulimit -s and set it to a new value, for example ulimit -s 16384 .

Here's the link with the 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()) 
+78
Dec 01 '09 at 12:47
source share

stacks for threads are often smaller. You can change the default value at connection time, or change at runtime. For reference, some default values ​​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 64KB
  • HP-UX 11 16KB
+34
Dec 01 '09 at 13:01
source share

platform-specific, binding-dependent, ulimit-dependent, parameter-dependent .... It is not specified at all, and many static and dynamic properties can affect it.

+12
Dec 01 '09 at 12:44
source share

Yes, there is an opportunity. C and C ++ standards do not dictate things like stack depth, usually this is a problem with the environment.

Most decent development environments and / or operating systems will allow you to adapt the stack size to the process either by reference or by load time.

You should indicate which OS and development environment you use for more targeted assistance.

For example, in Ubuntu Karmic Koala, 2M and 4K are reserved for gcc by default, but this can be changed when you link the program. Use the --stack ld option to do this.

+3
Dec 01 '09 at 12:44
source share

I'm not sure what you mean by doing the first depth search in 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 the stack that is allocated from the heap.

+2
Dec 01 '09 at 13:09
source share

I just ran out of work on the stack, it was a database, and there were threads on it, basically the previous developer threw a large array onto the stack, and in any case, the stack was low. The software was compiled using Microsoft Visual Studio 2015.

Despite the fact that the stack ended in the stream, it was silent and continued, but the stack only overflowed when access to the data contents on the stack came.

The best advice I can give is not to declare arrays on the stack, especially in complex applications and especially in threads, use heap instead. What is this for;)

Also keep in mind that this may not work right after the stack is declared, but only with access. I assume that the compiler declares the stack to be β€œoptimistic”, i.e. Suppose the stack is declared and is large enough until it starts using it, and then finds out that the stack does not exist.

Different operating systems may have different stack declaration policies. Please leave a comment if you know what this policy is.

+2
Aug 18 '17 at 18:13
source share



All Articles