catch stack overflow

Hi I'm working on a task which requires us to excecute a recursive function and then when we've reached the stacks limit clear the stack and then excecute another recursive function. is this possible or has our tutor given us an impossible task?
to clear the stack I ofcourse just call return. And i've read on some forums which says that you can't catch stack overflow which means I need to check if there is sufficient memory to do another recursion. how do I do that?
Last edited on
That task seems weird.

In general, there isn't really a way to know how much stack space you have, how much you've used, or how much is left.

Having said that, I _could_, if I wanted, figure it out programmatically on Linux. (involving "knowing" the address of the bottom of the stack, my process' stack size limit (getrlimit), getting the current stack pointer (taking address of a local variable), computing how much stack space an instance of the function takes (take address of local variable, call function recursively once, take address of same local variable, subtrace), and then doing the math.

But that is quite bizarre.
The recursive task you're describing would cause a page fault before you see a "Stack Overflow" error or cause a Segfault of anykind. I'm curious as to why your teacher would bother covering this as well, modern MMU's have ways of dealing with this issue to the point where you'll almost never see them.
Topic archived. No new replies allowed.