简体   繁体   English

未处理的异常访问冲突读取位置/使用结构向量

[英]unhandled exception access violation reading location / using vector of struct

I face the "unhandled exception access violation reading location", after i searched and realize that the problem reason from using vectors, but still i didn't solve it yet. 搜索并意识到使用向量导致问题的原因后,我遇到了“未处理的异常访问冲突读取位置”,但仍然没有解决。

in .hi defined the following: .hi中的定义如下:

struct inside
{ 
  double y;
  vector <int> values;

};

struct outside
{ 
     int x;
     vector <inside> subInfo;
     vector <double> d;


};

in .cpp: 在.cpp中:

   vector <outside> mainInfo;

       ....
   // method_inti called one time just ...
   method_inti()
   {
        for(int i=1;i<=4;i++)
         {
            outside obj;
            obj.x = i;
            mainInfo.push_back(obj);
         }

   }


      ....

// method fill called many times (very large number of calls .. ) //方法填充被调用多次(非常多的调用..)

  method_fill()
  {
      ... 

     for(i = 1; i <= 4; i++)
     {
        for (int j=0;j< mainInfo.size();j++) // here surly size = 4
        {
           if(mainInfo[j].x == i)
            {    

                inside obj;
                obj.y = i+1;
                obj.values.push_back(10.0);

                mainInfo[j].d.push_back(5.0);
                mainInfo[j].subInfo.push_back(obj);     

          }

       }
     }
       ...

   } 

   // update n val method also called many times..        
    updateNval_method(int t)
     { 
         double n;

        for (int i=0; i< mainInfo.size();i++) 
        {
           if(mainInfo[i].x == t)
            { 
               n= mainInfo[i].d[(mainInfo[i].d.size())-1];
            }
         }

     }

In the line n= mainInfo[i].d[(mainInfo[i].d.size())-1]; 在行n= mainInfo[i].d[(mainInfo[i].d.size())-1]; you're subtracting 1 from d.size() to get an array index, but you have no guarantee that d.size() >= 1. 您要从d.size()中减去1以获得数组索引,但不能保证d.size()> = 1。

If d happens to contain no elements, you're trying to access an array index of -1, which will throw that exception. 如果d恰好不包含任何元素,则您尝试访问的数组索引为-1,这将引发该异常。

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

相关问题 未处理的异常。 访问冲突读取位置 - Unhandled exception. Access violation reading location ****访问冲突读取位置处的未处理异常***** - Unhandled exception at **** Access violation reading location ******* 未处理的异常:访问冲突读取位置 - Unhandled Exception: Access violation reading location …访问冲突读取位置的未处理异常 - Unhandled exception at … Access violation reading location memcpy“未处理的异常”“访问冲突读取位置” - memcpy “unhandled exception” “access violation reading location” 奇怪的未处理异常; 访问冲突读取位置 - Strange unhandled exception; Access violation reading location 未处理的异常; 使用MSVS 9.0(VS 2008)的访问冲突读取位置 - Unhandled exception; Access violation reading location with MSVS 9.0 (VS 2008) 未处理的异常:读取内存位置的堆和访问冲突已损坏 - Unhandled exception: Corrupted heap and access violation reading memory location 未处理的异常:访问冲突读取位置 0x00000004 - a Unhandled exception :Access violation reading location 0x00000004 使用refprop.dll调用的访问冲突读取位置处的未处理异常 - Unhandled exception at Access violation reading location using refprop.dll call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM