I am making extensive use of stl vectors to manage memory (de-) allocation of large arrays of data. In particular I am generating perspective projections of anatomical structures from a large number of angles (180 in 2 degree steps), processing and analysing the results. The results are used to define radiation fields for radiotherapy.
It seems that if the arrays exceed a certain size (>3 anatomical structures) that the memory overflows. In particular the error is as follows
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
This is the a result of using at, which does bounds checking, rather than the faster [] operator. If I have <=3 structures the error does not occur.
I have tracked the error down to the following block of code
bool dicomCP::assignBeamlet(int beamletNumber, Beamlet &b1)
{
//std::cout << "\nInside dicomCP::assignBeamlet (int, Beamlet &)\n";
if (!this->isSet)
{
this->beamlets.at(beamletNumber).setLeftRight(b1.left,b1.right);
this->isSet=true;
return true;
}
else if (!this->beamlets.at(beamletNumber-1).isOpen())
{
return false;
}
// left (outside) min(left) and right (outside) max(right) leaves
else if ((this->beamlets.at(beamletNumber-1).right-b1.left >EPSILON2)&&(b1.right-this->beamlets.at(beamletNumber-1).left>EPSILON2))
{
if (this->beamlets.at(beamletNumber).open) return false;
else if (!this->beamlets.at(beamletNumber).open)
{
this->beamlets.at(beamletNumber).setLeftRight(b1.left,b1.right);
this->beamlets.at(beamletNumber).isAssigned=true;
this->isSet=true;
return true;
}
}
else return false;
}
Note that if the "this->isSet=true;" lines are commented out the error does not manifest itself regardless of the number of structures :yes it works with 6! The "isSet" boolean value is used to determine which objects have been set, and hence which objects need to be written out to a data file for further processing.
System and sodtware:
gcc (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839] SuSE 11.2 64bit Intel Celsius with 4 Xeon 2.66GHz CPUs and 4GB RAM Eclipse CDT (IDE) 64 bit Build 20100218-1602