How to determine maximum stack usage?
The most common way to determine the deepest stack usage is to initialize the stack memory with some known but unusual value, then periodically (or at the end of a big test run) see where that pattern stops.
This is exactly how the IAR IDE determines the amount of stack used.
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())
How do I find the maximum stack size?
You can query the maximum process and stack sizes using getrlimit
. Stack frames don't have a fixed size; it depends on how much local data (i.e., local variables) each frame needs.
To do this on the command-line, you can use ulimit.
If you want to read these values for a running process, I don't know of any tool that does this, but it's easy enough to query the /proc filesystem:
cat /proc/<pid>/limits
Maximum Stack Size for C/C+ Program?
I tried below program.
int main()
{
int nStack[519492];
cout<<"Okay!";
return 0;
}
The output:
Okay!
But if I increase the array size by 1 byte, the program crashes.
int main()
{
int nStack[519493];
cout<<"Okay!";
return 0;
}
Output:
No output. Program crashes.
How to know the stack size limit of .exe program?
From this answer, to check the stack size you need to install Visual Studio and use the visual studio tool dumpbin
. Usually there is a script that can be run to bring a command prompt windows with all the visual studio tools in path, this is vcvarsall.bat or "x64 Native Tools Command Prompt for VS 2019" (or something like that) from the start menu.
Run
dumpbin /headers executable.exe
This will return a long output. In that output, look for OPTIONAL HEADER VALUES
and in that part, there will be a size of stack reserve
. The default stacksize is written as 100000
(bytes) i.e. 1 Megabytes.
You can change the stacksize of an executable by using editbin
, also provided by Visual Studio:
editbin /stack:N executable.exe
Here N is the number in bytes for the stacksize.
Related Topics
Check If a Variable Exists in a List in Bash
Making Cmake Print Commands Before Executing
Why Does Sed Fail with International Characters and How to Fix
Create Zip File and Ignore Directory Structure
Grep a Large List Against a Large File
Need to Remove the Count from the Output When Using "Uniq -C" Command
Rsync Copy Over Only Certain Types of Files Using Include Option
What Do the Numbers in /Proc/Loadavg Mean on Linux
How to Use Ioctl() to Manipulate My Kernel Module
How to Script a "Yes" Response for Installing Programs
Piping Text to an External Program Appends a Trailing Newline
Should I Use Libc++ or Libstdc++
Why Do We Need a Bootloader in an Embedded Device
How to Tell in Linux Which Process Sent My Process a Signal