I have a recursive function that declares at least 50KB of local variables in one call and a stack overflow might be likely with the number of recursive steps I'll need. However, by the time the function is called within itself, these variables aren't needed anymore. Can I delete them, before the function call, to prevent them from consuming excess stack memory? I want to avoid using memory from the heap.
If you must use recursion, then put those recyclable variables inside a struct
, and pass that reusable struct
instance (instantiated at the outermost layer of recursion) by reference to your recursive calls.