如何捕获c++中的内存不足异常?
谁能告诉我如何捕获内存不足异常?
例如。
try
{
while(true)
{
int i = new int;
}
}
catch( ? <--- what should be put here?)
{
//exception handling
}
还有这个,
queue<int> q;
try
{
while(true)
{
q.push(10);
}
}
catch( ? <---- what should be put here?)
{
//error handling
}
can anyone please tell me how to catch out of memory exception?
for ex.
try
{
while(true)
{
int i = new int;
}
}
catch( ? <--- what should be put here?)
{
//exception handling
}
and also this,
queue<int> q;
try
{
while(true)
{
q.push(10);
}
}
catch( ? <---- what should be put here?)
{
//error handling
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
捕获
std::bad_alloc
。您还需要一个处理错误的策略,因为您想做的许多事情都需要内存(即使只是在关闭之前向用户显示错误)。一种策略是在启动时分配一块内存,并在尝试使用更多内存之前在异常处理程序中删除它,以便有一些内存可供使用。
Catch
std::bad_alloc
.You will also need a strategy for handling the errors, since many of the things you'd like to do will require memory (even if it's only to display an error to the user before shutting down). One strategy is to allocate a block of memory at startup, and
delete
it in the exception handler before attempting to use more memory, so that there is some available to use.正如其他人所指出的,您想要捕获的是
std::bad_alloc
。您还可以使用catch(...)
或catch(exception& ex)
来捕获任何异常;后者允许在异常处理程序中读取和使用异常数据。Mark Ransom 已经指出,当程序无法分配更多内存时,即使打印错误消息也可能失败。考虑以下程序:(
我强烈建议将该程序编译为 32 位,以避免在 64 位计算机上运行系统内存不足。32 位程序不能分配超过 4 GB 的内存,或者Windows 上默认为 2 GB。)
当第一个
bad_alloc
被抛出无限while
循环时,控制权将传递给catch
> 阻止,但程序仍然失败未处理的异常。为什么?当尝试打印到 cerr 时,另一个bad_alloc
被抛出异常处理程序内。您可以使用调试器来验证这一点:在catch(bad_alloc& ex)
行设置断点,在调试器中运行程序,然后在到达断点后单步执行每个语句。cerr
语句中将引发bad_alloc
异常。因此,为了正确处理内存不足的情况,您需要留出一些内存,以便可以在退出之前打印错误消息。否则,程序在尝试打印错误消息时将因未处理的异常而崩溃。为此,您可以分配一块在异常处理程序中释放的内存块,如 Mark Ransom 建议的那样:
As others have noted, what you want to catch is
std::bad_alloc
. You can also usecatch(...)
orcatch(exception& ex)
to catch any exception; the latter allows the exception data to be read and used in the exception handler.Mark Ransom had already pointed out that when the program cannot allocate any more memory, even printing an error message may fail. Consider the following program:
(I strongly recommend that the program be compiled as 32-bit to avoid running the system out of memory on a 64-bit machine. 32-bit programs cannot allocate more than 4 GB of memory, or 2 GB by default on Windows.)
When the first
bad_alloc
gets thrown in the infinitewhile
loop, control is passed to thecatch
block, but the program still fails with an unhandled exception. Why? Anotherbad_alloc
is thrown inside the exception handler while trying to print tocerr
. You can verify this by using a debugger: Set a breakpoint at thecatch(bad_alloc& ex)
line, run the program in the debugger, then step through each statement once you reach the breakpoint. Abad_alloc
exception will be thrown in thecerr
statement.As such, to properly handle an out-of-memory scenario, you need to set aside some memory so that you can print an error message before exiting. Otherwise, the program will just crash on an unhandled exception while trying to print the error message. To do so, you can allocate a block of memory that is deallocated in the exception handler, as Mark Ransom suggested:
作为注释,您应该阅读 bdonlan 的评论。对 cerr 的调用很可能会失败。马克·兰塞姆(Mark Ransom)在回答中的建议是缓解这个问题的好策略。
As a note you should read bdonlan's comment. The call to
cerr
may very well fail. Mark Ransom's suggestion in his answer is a good strategy to mitigate this issue.您应该
catch
一个std::bad_alloc
类型的对象。或者,您还可以使用
new
的nothrow
版本:当您使用此版本时,如果
new
失败,则不会引发异常。相反,它只是返回NULL
,您需要在继续操作之前检查该值。You should
catch
an object of typestd::bad_alloc
.Alternatively, you can also use a
nothrow
verison ofnew
as:When you use this, no exception is thrown if the
new
fails. Instead,it simply returnsNULL
which you check before proceeding further.