C++ bad_alloc 在内存位置异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10940067/
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
C++ bad_alloc at memory location Exception
提问by user272671
Unhandled exception at 0x758cd36f in OSGP.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0028ef70..
OSGP.exe 中 0x758cd36f 处未处理的异常:Microsoft C++ 异常:std::bad_alloc 在内存位置 0x0028ef70..
I am trying to execute the code below in Visual Studio. However, i keep running into the exception above. I added a try catch to aid me in catching the error but to no avail it seems. I believe the problem is related to the following from the output window
我正在尝试在 Visual Studio 中执行下面的代码。但是,我一直遇到上述异常。我添加了一个 try catch 来帮助我捕获错误,但似乎无济于事。我相信问题与输出窗口中的以下内容有关
First-chance exception at 0x758cd36f in OSGP.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0019f2f4..
First-chance exception at 0x758cd36f in OSGP.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0019ec84..
First-chance exception at 0x758cd36f in OSGP.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
The thread 'Win32 Thread' (0x16dc) has exited with code 0 (0x0).
The program '[448] OSGP.exe: Native' has exited with code 0 (0x0).**
Here's the code:
这是代码:
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <new>
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int flag = false;
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Node> root;
try
{
root = osgDB::readNodeFile("cessna.osg");
viewer.setSceneData(root.get());
}
catch(bad_alloc)
{
if (flag) cout << "a bad_alloc exception just occured";
}
return viewer.run();
}
回答by Ryan Erickson
std::bad_alloc is commonly thrown when the program doesn't have enough memory to complete the requested action.
当程序没有足够的内存来完成请求的操作时,通常会抛出 std::bad_alloc 。
Possible problems:
可能的问题:
- cessna.org too large to be processed on the machine you are running this on
- bad data/logic in cessna.org causing it to try to allocate an infinite amount of memory
- cessna.org 太大,无法在您运行它的机器上处理
- cessna.org 中的错误数据/逻辑导致它尝试分配无限量的内存
But it is impossible to say with the information given.
但是根据所给的信息是不可能说出来的。
回答by Robin Rowe
A bad alloc may also get thrown if there's a pointer to invalid memory being passed in an object's constructor.
如果在对象的构造函数中传递了指向无效内存的指针,也可能会抛出错误的分配。
回答by Ali
I should qualify this response by disclosing that my coding expertise can be generously described as novice.
我应该通过披露我的编码专业知识可以被慷慨地描述为新手来限定这个回应。
I had a similar error with some code that I was running. The cause appeared to be when I was declaring a new array like so:
我正在运行的一些代码也有类似的错误。原因似乎是当我像这样声明一个新数组时:
path_chr = new char [path.size()+1];
I was doing this many times (millions?) in my code. It looks like I eventually ran out of memory. The fix was deleting the variable when I was done.
我在我的代码中做了很多次(数百万次?)。看起来我最终耗尽了内存。修复是在我完成后删除变量。
delete [] path_chr;
Never had problems after that.
之后就再也没有问题了。
回答by mahela007
I find that this happens when you try to read past the end of an array.. That is, if you try to access more elements than the number of elements present in the array.
我发现当您尝试读取数组末尾时会发生这种情况。也就是说,如果您尝试访问比数组中存在的元素数量更多的元素。