Currently, I am working in C++ on a recursive function that to work, needs to be passed a large array and several constant variables. Here is the structure of the recursion:
void recurse_objects(std::vector<Object> &objects, int i, int j, double merge_distance, double size_enclosure, double timestep){
//operations performed
//recursion performed
if(j+1 < objects.size()){
recurse_objects(objects, i, j+1, merge_distance, size_enclosure, timestep);
}else if(i+2 < objects.size()){
recurse_objects(objects, i+1, i+2, merge_distance, size_enclosure, timestep);
}
//more operations performed
}
The function recurses through each unique pair (i, j) of objects. The problem is, at the amount of objects I am working at (500-2000 objects), the function segmentation faults due to the stack filling up - it works for under 350 objects.
It feels like if I could store the array and constant variables somewhere else, I could save room and be able to run computation on those last few objects. Is there a common fix to this problem? The only ones I could think of are using global variables - not feasible since values are dictated by system input, to batch, which would completely kill the efficiency, or just to change the program to a non-recursive one.