limiting conditions of recursive function
A general method for avoiding a stack overflow is to include what's called a bootstrap condition within the recursion. It's some condition that gets hit every time the function calls itself. You set the condition to something that causes the function to return when some state is reached, thereby unwinding the stack. If you have a lot of recursion, which exceeds the stack size, there may be a setting in the compiler you can use to increase the stack size, since that's managed by the C/C++ runtime.