-
-
Save JeOam/f99f914046a373ec345c8175ffe3bb51 to your computer and use it in GitHub Desktop.
C++ Primer Notes
一旦一个程序用光了它所有可用的内存,new 表达式就会失败。默认情况下,如果 new 不能分配所要求的内存空间,它会抛出一个类型为 bad_alloc 的异常。我们可以改变使用 new 的方式来阻止它抛出异常:
int *p1 = new int; // 如果分配失败,new 抛出 std::bad_alloc
int *p2 = new (nothrow) int; // 如果分配失败,new 返回一个空指针bad_alloc 和 nothrow 都定义在头文件 new 中。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

将
failbit和badbit复位,但保持eodbit不变:警告:如果程序崩溃,输出缓冲区不会被刷新。