Noted that the default stack size from the compiler is 1MB and to increase the stack size, you have to manually enter the stack size in the linker>system configuration. Is there a way to see what is the stack size I need based on the code that I've written so I can manually enter the stack size without giving it too much?
-
2If you can't figure out required stack size then it is a good idea to switch to allocating objects with dynamic storage duration. – user7860670 May 23 '23 at 07:38
-
2That is an undecidable problem for all but very trivial examples. I would say that if you cannot tell whether 1MB is enough, it's too much already. – Quimby May 23 '23 at 07:38
-
*Is there a way to see what is the stack size I need based on the code that I've written* -- I think it's time to rethink your program's design. Things such as determining *why* your program requires so much stack space. – PaulMcKenzie May 23 '23 at 07:40
-
2FWIW, I think I've had to worry about this exactly never in three decades of C++. If you run out of stack, it usually means that you either have a bug or need a rethink. – molbdnilo May 23 '23 at 08:39
-
possible duplicate https://stackoverflow.com/q/389219/4117728 – 463035818_is_not_an_ai May 23 '23 at 09:35
-
I suspect that this is [XY problem](https://xyproblem.info/). Why do you think you need to increase stack size? What is actual/first problem you are struggling with? If your code has some bug which leads to infinitive recursion increasing stack size will not help. – Marek R May 23 '23 at 09:42
-
I agree with the other comments here, but in a 64 bit build, there's no need to skimp on stack size, since there is plenty of address space and ram is only committed as needed. But as others are saying, why do you need so much? – Paul Sanders May 23 '23 at 14:48
-
I cant really say why i need so much stack size due to the confidentiality of the project. But basically I have tons of multi-dimensional arrays that consumed too much stack size. Example, If I used a sizeof operator. It basically consumed 14 million bytes of memory. Hence it requires me to set the stack size manually in the system configuration to prevent stack overflow – ThomasEdition May 23 '23 at 15:54
2 Answers
In general you cannot know how much stack is needed before the program is executed. Consider this contrived example:
void foo() {
int x; // on the stack
std::cin >> x;
if (x) foo();
std::cout << x;
}
In absence of optimizations that would transform the recursion into a loop, no matter how big your stack foo
can exceed it or not depending on user input.
In general it is not possible to decide beforehand how much stack is going to be used.

- 109,796
- 11
- 89
- 185
This is a common concern for embedded software. It is very difficult to make an accurate estimate on the needed stack size, but there a few common approaches:
- Do not to allocate larger objects on the stack
- Store a "magic number" at the end of the stack and have a mechanism to detect if this is ever overwritten.
- Write a fixed byte pattern in the stack area at the beginning of the program and look at how much of this was overwritten at the end to see how much stack space a given run of the program has used.
It may vary how feasible these experimental methods are on non-embedded systems, but it should be fairly simple to estimate the stack use at any given time by storing the (approximate) initial location of the top of the stack and then comparing the current location to that.
The following code shows that approach:
#include <stdio.h>
void *stack_base;
void print_stack_size()
{
char c; // "c" is allocated on the stack at the current position
printf("Used stack: %td\n", stack_base-(void *)&c); // Estimate of current usage (note that stack usually grows downwards (towards lower addresses)
}
int recursive_call(int n)
{
int result = 1;
if(n > 1)
{
print_stack_size();
result = result + recursive_call(n-1);
}
return result;
}
int main()
{
char a;
stack_base = &a; // "a" is stored on the stack and at this point we should be close to the top of the stack area
recursive_call(20);
return 0;
}
If the compiler optimization has not been too aggressive, this should show the (approximate) stack usage through the series of recursive function calls.
Of course, this only gives a snapshot at a given point in time, but with a bit of thought it should be possible to locate some worst case candidates and get an idea of the stack usage. In any case, the stack size should always be set with a healthy margin.

- 5,641
- 10
- 27