C++ “std::bad_alloc”:我是否使用了太多内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8378797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"std::bad_alloc": am I using too much memory?
提问by Rooster
The message:
消息:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
I looked at the gdb backtrace and this is the lowest level method in there that I implemented myself:
我查看了 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;
}
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.
您在上面看到的 cout 语句表明它在 7000 多次迭代后终止。上述函数仅在我的应用程序结束时调用一次。我在调用上面的函数之前调用了一个非常相似的函数,这不会导致问题。
采纳答案by Rooster
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.
我的问题原来是this->meshPoints.getNormalForVertex(i)
访问一个数组(或向量,我不记得了),其长度小于this->meshPoints.getNumFaces() * 3
. 所以它访问越界。
回答by Matteo Italia
(moving/expanding from the comments)
(从评论中移动/扩展)
Since you are allocating a new array every time without deallocating it, you have a massive memory leak, i.e. you continue to ask memory to the system without ever giving it back. Eventually the space on the heap finishes, and at the next allocation all you get is a std::bad_alloc
exception.
由于您每次都分配一个新数组而不释放它,因此您会出现大量内存泄漏,即您继续向系统请求内存而不将其归还。最终堆上的空间用完,在下一次分配时,你得到的只是一个std::bad_alloc
例外。
The "C-style" solution would be to remember to deallocate such memory when you don't need it anymore (with delete[]
), but, this is (1) error-prone (think e.g. if you have multiple return paths inside a function) and (2) potentially exception-unsafe (every instruction becomes a potential return path if you have exceptions!). Thus, this way should be avoided.
“C 风格”的解决方案是记住当你不再需要它时释放这样的内存(使用delete[]
),但是,这是(1)容易出错(例如,如果你在一个函数中有多个返回路径) (2) 潜在的异常不安全(如果有异常,每条指令都会成为潜在的返回路径!)。因此,应该避免这种方式。
The idiomatic C++ solution is to use either smart pointers- small objects that encapsulate the pointer and deallocate the associated memory when they are destroyed - or standard containers, that do more or less the same thing but with copy semantics and some more bells and whistles (including storing the size of the array inside them).
惯用的 C++ 解决方案是使用智能指针- 封装指针并在它们被销毁时释放相关内存的小对象 - 或标准容器,它们或多或少地做同样的事情,但具有复制语义和更多的花里胡哨(包括在其中存储数组的大小)。
回答by Tim Mottram
I got this error trying to allocate a negative length array:
我在尝试分配负长度数组时遇到此错误:
double myArray = new double[-9000];
double myArray = new double[-9000];
Just in case it helps anyone.
以防万一它可以帮助任何人。