helios wrote: |
---|
He said new can always return 0, but I said it will only return 0 if passed std::no_throw. |
He probably use an environment where an alternative new-handler is installed by default. This happens for some older Visual Studio project settings... (IIRC VS 6.0 and earlier).
Nowadays, the default new handler throws by default.
helios wrote: |
---|
wouldn't the stack be unwound in such a way that the return value is never assigned to the pointer |
That's correct. The exception stops the excecution before the assignment of any what-so-ever value to anything. If the pointer did not had a value before, it still counts as "uninitialized" and all access to it must be avoided.
1 2 3 4 5 6
|
int* p;
try { p = new int; } catch (...) {}
// in case of out-of-memory, p will be uninitialized!
p = new (std::nothrow) int;
// p is initialized. Either a pointer or 0 (in case of out-of-memory)
|
tajendra wrote: |
---|
new only return 0 in case when its instructed for std::no_throw, in all other case new will throw an exception and will never return in case of throwing exception. |
Actually new just calls the operator new of the class or the global installed new handler (if no operator new is present). If the global new handler throws bad_alloc (which should be default), so will new.
Oh, by the way.. it's "std::nothrow" not "no_throw".
One more note: Passing std::nothrow does
guarantee that new will not throw
any exception, not only for the case of out-of-memory. If any installed new-handler throws something else, the program will terminate and not propagate the exception (In other words: operator new(int, nothrow_t&) has a throws() declaration).
Ciao, Imi.