gpt4 book ai didi

c++ - 如何在 C++ 中捕获内存不足异常?

转载 作者:IT老高 更新时间:2023-10-28 12:31:46 37 4
gpt4 key购买 nike

谁能告诉我如何捕捉内存不足异常?

例如

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
}

最佳答案

捕捉 std::bad_alloc .

您还需要一个处理错误的策略,因为您想做的许多事情都需要内存(即使只是在关闭之前向用户显示错误)。一种策略是在启动时分配一 block 内存,并在异常处理程序中delete尝试使用更多内存,以便有一些可用的内存。

关于c++ - 如何在 C++ 中捕获内存不足异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749066/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com