- Seems, you are right about the first example.
Second and third examples deal with POD type. And there initialization differences play role.
In the second example your structure left uninitialized. No problem appear.
In opposite, in third example structure does initialize, so you get first case.
Edit:
Then, operator new
itself can throw an exception. Standard (c++11 darft says):
If the new
expression terminates by throwing an exception, it may
release storage by calling a deallocation function (3.7.4.2). If the
allocated type is a non-array type, the allocation function’s name is
operator new
and the deallocation function’s name is operator delete
.
It's bit unclear, what authors wanted to express saying it may release storage. It seems to be implementation defined, if it is released.
Anyway, you can try using not-throwing new
version:
void *operator new (size_t size, std::nothrow_t) throw() {
return malloc(size);
}