exceptions

1
2
3
  p= new (nothrow) int[i];
  if (p == nullptr)
    cout << "Error: memory could not be allocated";

nothrow is an exception that will handle the p if it doesnt successfully allocated.
but what does line 2 and 3 doing?
nothrow new (which should not be used, by the way) returns null pointer if memory cannot be allocated (which cannot happen on majority of modern OS using virtual memory and lazy allocation).
Second line checks if pointer returned by nothrow new is null popinter and output message if it is and allocation failed.
Second line checks if pointer returned by nothrow new is null popinter and output message if it is and allocation failed. 

yes i know.i mean, does nothrow already handles the situation in case the p fails to be allocated?
Last edited on
does nothrow already handles the situation in case the p fails to be allocated?
It does not handle situation. It suppresses (or does not emit in first place) exception if allocation fails and returns null pointer. YOu still need to handle failure: either by capturing or not capturing exception with notmal new, or checking result with nothrow new.

I recommend reading this article explaining why memory failures are not generally detectable: http://www.gotw.ca/publications/mill16.htm
okay. thank you going to read it.
Topic archived. No new replies allowed.