I just wrote a recursive function, and it became clear to me that all the variables that I use inside the function will remain in memory until the recursion breaks. If I recurs many times or allocate large amounts of memory for variables that are not used when calling a sequential recursive function, can this lead to excessive memory usage?
eg. in the following, only vec2used in the next recursion, temp_intand temp_vecwill continue to occupy memory unnecessarily.
int recurse(std::vector<int> arg_vec) {
int temp_int i;
std::vector<int> temp_vec;
std::vector<int> vec2;
recurse(vec2)
return if (some condition met);
}
Should I allocate all the memory with the new commands and delete them before calling the function? Or is there some other way to deal with this