I have some code which is working as it should. However, sometimes my program has to close due to an error. I have tried to debug this error but am having several problems when I do this.
When I run in debug, I get a stack overflow error very soon after I start the program, much sooner than when I get an error in release.
I have narrowed down the point at which the error is thrown in debug, at it is when this recursive function is called the 3570th time.
void setRankAbove(vector<nodeStructure> &allNodes, int index, int rankToSet) {
if (rankToSet>=allNodes[index].rank) {
allNodes[index].rank = rankToSet;
if (allNodes[index].root != index) {
setRankAbove(allNodes, allNodes[index].parent, rankToSet + 1,debug);
}
}
}
Any ideas about why the program is crashing here and why it only crashes here in debug?
Anyway, getting back to the original problem, I have narrowed down the place at which that fails.
vector<vector<double>> backwardsAggregation(int inputRows, int inputCols, vector<vector<double>> &localCostsStepOne, vector<nodeStructure> &nodeMST,float sigma,bool debug) {
// create 2D array of local costs step 2
vector<double> localOneDim(inputCols);
vector<vector<double>> localCostsRootToLeaf(inputRows, localOneDim);
// factors defined in paper
double nodeFactor = (1 - exp(-1 / (sigma*sigma)));
double parentFactor = exp((double)(-0.5) / (sigma*sigma));
cout << "Step 3.2.1" << endl;
try{
aggregateStepTwo(nodeMST, nodeMST[0].root, localCostsStepOne, localCostsRootToLeaf, parentFactor, nodeFactor, debug, 0);
}
catch (const std::exception & ex) {
cout<< "exception"<< endl;
cout << ex.what() << endl;
}
cout << "Step 3.2.2" << endl;
return localCostsRootToLeaf;
}
It fails when calling aggregateStepTwo after a certain number of calls. It could potentially recursively call it 1,000,000 times. Is this another stack overflow?
If so how do I get rid of the stack overflows?
Thanks Rob
std::stackusing a simple structure representing the input and output parameters. You should probably go this way. The stack size is limited. Well the dynamically allocatable memory is limited as well, but way more available usually anyways.setRankAboveto an iterative algorithm, then the stack will not be used for processing.