Skip to content

Instantly share code, notes, and snippets.

@JeOam
Created June 14, 2018 10:23
Show Gist options
  • Save JeOam/f99f914046a373ec345c8175ffe3bb51 to your computer and use it in GitHub Desktop.
Save JeOam/f99f914046a373ec345c8175ffe3bb51 to your computer and use it in GitHub Desktop.
C++ Primer Notes
@JeOam
Copy link
Author

JeOam commented Jul 13, 2018

一旦一个程序用光了它所有可用的内存,new 表达式就会失败。默认情况下,如果 new 不能分配所要求的内存空间,它会抛出一个类型为 bad_alloc 的异常。我们可以改变使用 new 的方式来阻止它抛出异常:

int *p1 = new int; // 如果分配失败,new  抛出 std::bad_alloc
int *p2 = new (nothrow) int; // 如果分配失败,new  返回一个空指针

bad_allocnothrow 都定义在头文件 new 中。

@JeOam
Copy link
Author

JeOam commented Jul 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment