" std :: bad_alloc":我使用太多内存? [英] "std::bad_alloc": am I using too much memory?

查看:466
本文介绍了" std :: bad_alloc":我使用太多内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

消息:

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.

这篇关于&quot; std :: bad_alloc&quot;:我使用太多内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆