C/C++ maximum stack size of program on mainstream OSes
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())
Default stack size
The default size comes from the .exe, not the OS.
From MSDN:
The default size for the reserved and initially committed stack memory
is specified in the executable file header.
Specifically, the stack reserve and commit sizes are specified in the IMAGE_OPTIONAL_HEADER
structure in a PE file. This can usually be set to a specific value with a linker parameter. With the MinGW toolchain you can try something like -Wl,--stack,52428800
as a gcc parameter. This option might exist in the IDE you are using, just look for build and/or linker settings.
This applies to the first thread, other threads can override the default if you specify a non-zero value when you call CreateThread
.
How can you find out the maximum size of the memory stack for a C++ program on linux? (gnu compiler)
Who controls the default maximum stack size; OS or compiler?
The compiler typically. The OS/hardware does limit it to a certain extent. Default is 8MB on linux IIRC. Think of ulimit -s
on Linux (to change stack sizes).
Is the default maximum scaled according to total memory? (ie a machine with 2gb memory would have larger default size than a machine with only 512mb) For this example both machines are same os/compiler setup, just different amounts of system RAM.
No. Until and unless you do it yiurself.You can alter stack sizes via compiler switches.
ld --stack=<STACK_SIZE>
or
gcc -Wl,--stack=<STACK_SIZE>
The C++ Standard's take on the issue of stacks and heaps:
The standard is based on an abstract machine and does not really concern itself with hardware or stacks or heaps. It does talk about an allocated store and a free store. The free store is where you'd be if you are calling new (mostly). FWIW, an implementation can have only one memory area masquerading as both stack and heap when it comes to object allocation.
Your question, therefor, boils down to be an implementation specific issue rather than a language issue.
Hope this helps.
what is the size of stack in VC++?
As Andreas Brinck states in his related answer:
In VC++ 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.
This stack size limit can be modified using:
Project → Properties → Configuration Properties → Linker → System → Stack Reserve Size.
Related Topics
Why Do We Need Virtual Functions in C++
What Are the Differences Between Struct and Class in C++
Capture Characters from Standard Input Without Waiting For Enter to Be Pressed
Why Can't Variables Be Declared in a Switch Statement
When Should I Write the Keyword 'Inline' For a Function/Method
Output Unicode Strings in Windows Console App
Pointer to Class Data Member "::*"
Why Does C++ Code For Testing the Collatz Conjecture Run Faster Than Hand-Written Assembly
How to Automatically Generate a Stacktrace When My Program Crashes
Single Quotes Vs. Double Quotes in C or C++
C++ Returning Reference to Local Variable
What Are the Main Purposes of Using Std::Forward and Which Problems It Solves
Meaning of 'Const' Last in a Function Declaration of a Class
What Are the Barriers to Understanding Pointers and What Can Be Done to Overcome Them
How to Convert a Std::String to Const Char* or Char*