I have a recursive function which can be written like:
void func(TypeName *dataStructure, LL_Node **accumulator) {
func(datastructure->left, accumulator);
func(datastructure->right, accumulator);
{
char buffer[1000];
// do some stuff
}
return;
}
I know that in reality the buffer is being allocated at the beginning of the function and putting the statement in a nested scope block doesn't actually use a new stack frame. But I don't want the compiler to allocate an exponential number of 1000-byte buffers at once, when they can be allocated and thrown away one at a time as each level returns.
Should I use outside global variables? A call to a helper function to force the buffer to be allocated after the recursive call? What I'm really fishing for here is advice on the cleanest, most C-idiomatic way of forcing this behavior.
Edit: One add-on question. If the exact same accumulator
will be passed to every call of func
, is it unheard of to leave the accumulator
pointer in a global variable rather than pushing it onto the stack with every call?