我被告知这样做,
int* i = new int();
Run Code Online (Sandbox Code Playgroud)
我无法检查我的指针为0. new如果失败则发送异常.但是,如果我不想出于原因使用异常会怎样.有没有办法检查我的指针是否正确分配?
NoD*_*und 10
阅读文档:http://www.cplusplus.com/reference/new/operator%20new/
(2)nothrow分配与上面的(1)相同,除了在失败时返回空指针而不是抛出异常.
以及示例:
std::cout << "2: ";
MyClass * p2 = new (std::nothrow) MyClass;
// allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
// and then constructs an object at the newly allocated space
Run Code Online (Sandbox Code Playgroud)