This is an excerpt of Stroustup's book, 3rd edition, at page 362 :
In principle, an exception is copied when it is thrown, so the handler gets hold of a copy of the original exception. In fact, an exception may be copied several times before it is caught. Consequently, we cannot throw an exception that cannot be copied. The implementation may apply a wide variety of strategies for storing and transmitting exceptions. It is guaranteed, however, that there is sufficient memory to allow new to throw the standard outofmemory exception, bad_alloc.
It means that if you run out of memory and a call to new() throws a std::bad_alloc, there will be some memory reserved to create, and make the correct number of copies of the std::bad_alloc object without throwing another std::bad_alloc as a result of the process of throwing the exception.
Normally, when you throw
an exception, it is a two step process:
The standard guarantees that in the case there is no longer any memory available to you, the system still has enough memory available to be able to throw
a std::bad_alloc
exception.
There are several strategies available:
In general, the runtime puts aside some space to copy exceptions without actually performing a memory allocation. So just re-using this space is fairly common.
it means if you enter new and there is not enough memory to allocate anything (ie you're totally out of memory), there will still be a pre-reserved buffer in the system that is big enough to create a 'bad_alloc' exception and throw it.
If you'd run out of memory, how would you expect the system to copy anything? That's why this is important in this section - it says that all exceptions need to be copied, but in the case where there is no memory, you can still get one of these special ones out to tell the rest of your app what's happened.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With