1

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

2
  • Write a minimal test application to reproduce the behavior you are observing and paste here. But the most probable explanation is that there's an issue with your code and not STL. Commented Dec 20, 2010 at 16:28
  • Take the time to review your other questions and accept some answers! Commented Dec 20, 2010 at 16:28

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Duh, of course. Thanks! A case of not seeing the wood for the trees!
2

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

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.