" std :: bad_alloc":我使用太多内存? [英] "std::bad_alloc": am I using too much memory?
问题描述
消息:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
我查看了gdb backtrace,这是最低级别的方法我实现了自己:
I looked at the gdb backtrace and this is the lowest level method in there that I implemented myself:
/*
* get an array of vec3s, which will be used for rendering the image
*/
vec3 *MarchingCubes::getVertexNormalArray(){
// Used the same array size technique as getVertexArray: we want indices to match up
vec3 *array = new vec3[this->meshPoints.getNumFaces() * 3]; //3 vertices per face
int j=0;
for (unsigned int i=0; i < (this->meshPoints.getNumFaces() * 3); i++) {
realVec normal = this->meshPoints.getNormalForVertex(i);
// PCReal* iter = normal.begin();
if (normal.size() >= 3) {
array[j++] = vec3(normal[0], normal[1], normal[2]);
}
cout << i << " ";
}
return array;
}
您在上面看到的cout语句表明它在7000+次迭代后终止。
上面的函数只在我的应用程序结束时调用一次。
The cout statement you see above indicates that it terminates after 7000+ iterations. The above function is called only once near the end of my application. I call a very similar function before calling the above, that doesn't cause problems.
推荐答案
我的问题原来是这样的: this-> meshPoints.getNormalForVertex(i)
访问长度小于 this->的数组(或向量, ; meshPoints.getNumFaces()* 3
。所以它正在访问超出边界。
My problem turned out to be that this->meshPoints.getNormalForVertex(i)
accesses an array (or vector, I can't remember) whose length is less than this->meshPoints.getNumFaces() * 3
. So it was accessing out of bounds.
这篇关于" std :: bad_alloc":我使用太多内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!