简体   繁体   中英

c++ stl vector causing memory overflow?

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

Apparently, you are accessing the elements outside of container. It's impossible to say from this code, whether the indexes are correct or not, walk through this code in debugger and you will see. The suspicious looking piece: this->beamlets.at(beamletNumber-1).isOpen() What if beamletNumber is 0? You get invalid index.

My guess is that you are passing beamletNumber==0, which is then made (unsigned)-1, or in other words a very large number.

at(largenumber) then throws

at() is throwing the exception. If the index is out of range, it throws an out_of_range exception.

Check if (beamletNumber/beamletNumber-1) correponds to an index in the vector where an element exists. at() checks it and is throwing an exception.

An exception of class out_of_range is used to report that an argument value is not in the expected range, such as when a wrong index is used in an array-like collection or string.

you are using both this->beamlets.at(beamletNumber-1) and this->beamlets.at(beamletNumber).

this->beamlets.at(beamletNumber-1) suggests you're treating the vector with 1-based indices while this->beamlets.at(beamletNumber) suggests 0-based indices.

with 1-based indices this->beamlets.at(beamletNumber) will certainly give an out-of-range error.

with 0-based indices this->beamlets.at(beamletNumber-1) will certainly give an out-of-range error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM