尝试分配一个非常大的数组,可能会抛出 std::bad_alloc 异常 int* largeArray = new int[1000000000000]; // 模拟内存分配失败 delete[] largeArray; // 释放内存 } catch (const std::bad_alloc& e) { // 捕获 std::bad_alloc 异常,输出错误信息 std::cerr << "Memory allocation failed...
It's pushing the limits, If there are few other demands for memory from the program, it *might* succeed. Using VC++ 2008 and running under XP 32-bit, a Debug build will fail when the allocation is attempted but a Release build will succeed.Wayne ...
#include <iostream> #include <new> int main() { try { while (true) { new int[100000000ul]; } } catch (const std::bad_alloc& e) { std::cout << "Allocation failed: " << e.what() << '\n'; } } The output should be like this −...
It's pushing the limits, If there are few other demands for memory from the program, it *might* succeed. Using VC++ 2008 and running under XP 32-bit, a Debug build will fail when the allocation is attempted but a Release build will succeed.Wayne ...