4

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 ?

nikau6
  • 922
  • 9
  • 16
  • 5
    See this: http://stackoverflow.com/questions/7277637/new-stdnothrow-vs-new-within-a-try-catch-block – NathanOliver Jan 06 '17 at 13:58
  • `new` is guaranteed to not return `nullptr`. It isn't *forbidden* to check if the pointer returned by `new` is `nullptr` it's just unnecessary. – François Andrieux Jan 06 '17 at 14:01
  • I'm curious what reasons one would have to avoid using exceptions. – 0x5453 Jan 06 '17 at 14:17
  • nikau6, I was not implying this was a dupe. I just wanted to point you to some good information. – NathanOliver Jan 06 '17 at 14:27
  • 1
    @0x5453 Oh, there are reason: 1. exceptions are slow -> no-no in realtime applications that need to guarantee completion on a deadline, 2. an exception is basically a non-local combination of a `goto` and a `COMEFROM` (see INTERCAL) - in a way this makes an exception worse than the `goto` that we all despise and avoid... And 3., I believe there are coding guidelines out there that prohibit use of exceptions. – cmaster - reinstate monica Jan 06 '17 at 14:28
  • @NathanOliver I saw a message on my post telling me that my question was a possible duplicate and that I had to edit it to explain why it's not. So, I did it. I never thought that someone could think this was a dupe. – nikau6 Jan 06 '17 at 14:33
  • Oh. Now I see someone voted to close. Let me clear that. – NathanOliver Jan 06 '17 at 14:34
  • @NathanOliver Where did you see that ? Did the way I edited my question is not good ? I'm new on stack overflow, registered for a long time but using it recently. – nikau6 Jan 06 '17 at 14:40
  • 1
    @nikau6 No you are fine. All is good now. – NathanOliver Jan 06 '17 at 14:40
  • @NathanOliver Ok :-) – nikau6 Jan 06 '17 at 14:42
  • Closing as duplicate does not mean your question is deleted. Duplicates are kept for the purpose of letting future visitors find the correct answer quicker, so it is made for specifically this situation. – nwp Jan 06 '17 at 14:58
  • @NathanOliver Ok, I thought my question would be deleted if I didn't edit it. Should have erase the edit part ? – nikau6 Jan 06 '17 at 15:02
  • @nikau6 I would not. That way people who might want to close as a dupe can see your rebuttal. – NathanOliver Jan 06 '17 at 15:03
  • @NathanOliver Ok, thanks for your help :-) – nikau6 Jan 06 '17 at 15:04
  • @cmaster Playing devil's advocate -- not everyone hates `goto`, and any control flow structure is "basically a combination of `goto`s". And your third point is completely circular ;) – Quentin Jan 07 '17 at 21:40

1 Answers1

10

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
NoDataFound
  • 11,381
  • 33
  • 59
  • 8
    Beware that nothrow new can still throw if `MyClass`'s constructor throws. – François Andrieux Jan 06 '17 at 13:58
  • @FrançoisAndrieux So, what can I do to ensure that a constructor will never send a exception ? – nikau6 Jan 06 '17 at 15:15
  • 1
    @nikau6 You can't really stop it. Most compilers have a flag to disable exceptions but that won't do what you want here (`new` won't return `nullptr`, instead your process will likely terminate. The best you can do is only use constructors that cannot throw. Built-in types are trivially constructible. Many containers have default constructors that can't throw (unless given an allocator). See [noexcept specifier](http://en.cppreference.com/w/cpp/language/noexcept_spec) and [std::is_nothrow_constructible](http://en.cppreference.com/w/cpp/types/is_constructible). – François Andrieux Jan 06 '17 at 15:25
  • @FrançoisAndrieux Thanks! – nikau6 Jan 06 '17 at 15:30