消息:

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc

我查看了GDB回溯,这是我自己实施的最低级别方法:

/*
 * 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多个迭代后终止。以上功能仅在我的应用程序末尾接近一次。在调用上述情况之前,我调用非常相似的功能,这不会引起问题。

答案

我的问题原来是这样this->meshPoints.getNormalForVertex(i)访问一个数组(或向量,我不记得)长度小于this->meshPoints.getNumFaces() * 3。因此,它是从范围中访问的。

来自: stackoverflow.com