I understand that when something is thrown, the stack is ‘unwound’ to the point where it is caught, and the destructors of class instances on the stack in each function context are run (which is why you should not throw an exception from a destructor – you could end up throwing a second one)…but I wonder where in memory the object that I have thrown is stored while this happens?
Is it implementation dependent? If so, is there a particular method used by most popular compilers?
Yes, the answer is compiler-dependent.
A quick experiment with my compiler (
g++ 4.4.3) reveals that its runtime library first tries tomallocmemory for the exception and, failing that, attempts to allocate space within a process-wide “emergency buffer” that lives on the data segment. If that doesn’t work out, it callsstd::terminate().It would appear that the main purpose of the emergency buffer is to be able to throw
std::bad_allocafter the process has run out of heap space (in which case themalloccall would fail).The relevant function is
__cxa_allocate_exception:I don’t know how typical this scheme is.