I know it's not on topic in the purest form of on-topicness, but the way this is usually solved, is by changing your algorithm so that it uses a loop instead of recursion. Granted, that isn't always possible, in which case you have to fallback to recursion and allocation on the heap.

The first solution works great when you have only values that are further used when you go down the rabbit hole, like in this classic example for recursion:
Code:
int factorial(int n)
{
    if(!n)
        return 1;
    
    return n * factorial(n - 1);
}



Using a sophisticated compiler, you will end up with no recursion at all since your function would become victim of tail-call optimization. However, it's trivial to rewrite this using a loop:

Code:
int factorial(int n)
{
    int fact = 1;
    
    for(; n >= 1; n --)
        fact *= n;
        
    return fact;
}



Granted, for the sake of it I chose a really trivial example. But you should get the gist of it. Also take the following away: The stack isn't meant to hold huge allocations. Don't use it for that. Everything bigger than a hundred bytes probably belong better on the heap (there are exceptions, but yeah, rule of thumbs).


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com