Using new and the ability to check for a pointer to 0 (null)

I was told that,

int* i = new int();

I would not be able to check my pointer to 0. newsend an exception if it fails. But what if I don't want to use exceptions for reasons. Is there a way to check if my pointer is highlighted correctly?

+4
source share
1 answer

Read the document: http://www.cplusplus.com/reference/new/operator%20new/

(2) fuzzy distribution Same as above (1), except that if an error occurs, it returns a null pointer instead of throwing an exception.

Like an 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
+10
source

Source: https://habr.com/ru/post/1666001/


All Articles