I was told that by doing,
int* i = new int();
I would not be able to check my pointer for 0. new
send a exception in case it fails. But what if I don't want to use exceptions for reasons. Is there any way to check if my pointer is correctly allocated ?
Read the doc: http://www.cplusplus.com/reference/new/operator%20new/
(2) nothrow allocation Same as above (1), except that on failure it returns a null pointer instead of throwing an exception.
As well as example:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With