I have a massive recursive function that can easily overflow a system based on user input, and I wanted to know if there is a way to detect if you are about to run out of callstack space during runtime. Is there a way to check if I am going to blow the stack so I can terminate/do something else if I am about to?
-
2on linux if you ```#include
``` and say ```struct rlimit l; getrlimit(RLIMIT_STACK, &l); std::cout << l.rlim_cur;``` it wil give you the stack space. Perhaps use this size to see if user input is greater than this? (I am unsure if there is a standard on determining stack space). Or use a ```std::stack<>``` with a size constraint. – NeonFire Sep 15 '21 at 00:32 -
1Can you rewrite the function to be non-recursive? You should be able to use a loop and a `std::stack`. – Galik Sep 15 '21 at 01:41
-
@Galik I have the non-recursive version of that function already written. This is for learning purposes. I just want to know if it is possible. – Vince Sep 15 '21 at 03:45
-
@NeonFire I'm on a Windows system currently, but that's helpful information regardless. Thanks! – Vince Sep 15 '21 at 03:46
-
When doing software design you also should think about what resources (memory/stack space/....) you want to allocate to what part of your software. So the easiest way I can think of is don't detect but prevent. Think of how much stack memory you want to "eat up" in the first place and then set a maximum to your recursion depth and add that to your recursion end condition. If you can calculate the recursion depth up front even better because then you can prevent the recursion from even starting, leaving all those resources to other things in your system. – Pepijn Kramer Sep 15 '21 at 05:08
-
On Windows see https://stackoverflow.com/questions/48955546/how-do-i-find-out-the-size-of-available-stack-space – Dúthomhas Sep 15 '21 at 07:19
2 Answers
There are cases where we prefer to use recursive function. However, if the recursive function goes too deep in some environments, such as in Visual C++ code, an unwanted result might occur such as a stack-overflow.Same as yours for that purpose only we convert recursive function into while-loop with stack. You can check implementation here
Actually there is no common way to detect the system stack size because each and every compiler has the different stack size and some of them have adjustable stack size.
You can check different size as follow:
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
Since it's runtime error so you can't catch using try-catch
statement either but you can implement prevention logic.
#include <stdio.h>
// These will be set at the top of main()
static char * _topOfStack;
static int _maxAllowedStackUsage;
int GetCurrentStackSize()
{
char localVar;
int curStackSize = (&localVar)-_topOfStack;
if (curStackSize < 0) curStackSize = -curStackSize; // in case the stack is growing down
return curStackSize;
}
void MyRecursiveFunction()
{
int curStackSize = GetCurrentStackSize();
printf("MyRecursiveFunction: curStackSize=%i\n", curStackSize);
if (curStackSize < _maxAllowedStackUsage) MyRecursiveFunction();
else
{
printf(" Can't recurse any more, the stack is too big!\n");
}
}
int main(int, char **)
{
char topOfStack;
_topOfStack = &topOfStack;
_maxAllowedStackUsage = 4096; // or whatever amount you feel comfortable allowing
MyRecursiveFunction();
return 0;
}

- 19
- 5
-
1Per the comments under the question, the OP is already aware they can convert their function to use a stack object rather than the system stack. – nanofarad Sep 15 '21 at 04:05
-
Note that behaviour of subtracting address of one object with address of an unrelated object is technically undefined. – eerorika Sep 15 '21 at 06:55
Is there a way to detect a stack overflow during runtime? (c++)
There is no standard way to know how much stack space is available, nor is there a standard way to know how much stack has been consumed, nor is there a standard way to know how stack a function call would consume.
In conclusion: There is no standard way to detect a potential stack overflow.

- 232,697
- 12
- 197
- 326