简体   繁体   English

c + + stl向量导致内存溢出?

[英]c++ stl vector causing memory overflow?

I am making extensive use of stl vectors to manage memory (de-) allocation of large arrays of data. 我正在广泛使用stl向量来管理大数据数组的内存(de-)分配。 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. 特别是,我正在从大量角度(180度,以2度为步长)生成解剖结构的透视投影,并对结果进行处理和分析。 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. 看来,如果数组超过一定大小(> 3个解剖结构),则内存溢出。 In particular the error is as follows 特别是错误如下

terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check 抛出'std :: out_of_range'what()的实例后调用终止终止what():vector :: _ M_range_check

This is the a result of using at, which does bounds checking, rather than the faster [] operator. 这是使用at进行边界检查的结果,而不是使用更快的[]运算符。 If I have <=3 structures the error does not occur. 如果我有<= 3个结构,则不会发生错误。

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;" 请注意,如果“ this-> isSet = true;” lines are commented out the error does not manifest itself regardless of the number of structures :yes it works with 6! 注释掉所有行,无论结构的数量如何,错误都不会自行显现:是的,它可以与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. “ isSet”布尔值用于确定已设置了哪些对象,因此需要将哪些对象写出到数据文件中以进行进一步处理。

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 gcc(SUSE Linux)4.4.1 [gcc-4_4-branch版本150839] SuSE 11.2 64位Intel Celsius,具有4个Xeon 2.66GHz CPU和4GB RAM Eclipse CDT(IDE)64位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? 可疑的部分: this->beamlets.at(beamletNumber-1).isOpen()如果beamletNumber为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. 我的猜测是您要传递beamletNumber == 0,然后将其设为(无符号)-1,或者换句话说,是一个非常大的数字。

at(largenumber) then throws at(largenumber)然后抛出

at() is throwing the exception. at()引发异常。 If the index is out of range, it throws an out_of_range exception. 如果索引超出范围,则会引发out_of_range异常。

Check if (beamletNumber/beamletNumber-1) correponds to an index in the vector where an element exists. 检查(beamletNumber / beamletNumber-1)是否对应于元素存在的向量中的索引。 at() checks it and is throwing an exception. at()检查它并引发异常。

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. out_of_range类的异常用于报告参数值不在预期范围内,例如在类似数组的集合或字符串中使用了错误的索引时。

you are using both this->beamlets.at(beamletNumber-1) and this->beamlets.at(beamletNumber). 您同时使用this-> beamlets.at(beamletNumber-1)和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. this-> beamlets.at(beamletNumber-1)建议您使用基于1的索引处理向量,而this-> beamlets.at(beamletNumber)建议使用基于0的索引。

with 1-based indices this->beamlets.at(beamletNumber) will certainly give an out-of-range error. 使用基于1的索引时,this-> beamlets.at(beamletNumber)肯定会给出超出范围的错误。

with 0-based indices this->beamlets.at(beamletNumber-1) will certainly give an out-of-range error. 使用从0开始的索引,this-> beamlets.at(beamletNumber-1)肯定会给出超出范围的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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