无数的coder这样写:
char *pointer = new Object; if(pointer == NULL) { return false; }
而其中判断指针为NULL的这句是一句无效地判断,因为如果分配失败,STL会抛出异常,这段代码没有捕获异常,所以会core dump了。
让new操作符失败时不抛出异常:
char *pointer = new (std::nothrow) Object;