[英]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.