When I “drop” something, where is it stored in my memory?

I understand that when something is throw n, the stack is “unwound” to the point where it is caught, and the destructors of the class instances on the stack are launched in each functional context (that’s why you shouldn’t throw an exception from the destructor - you can end up throw out the second one) ... but I wonder where in memory the object I selected is saved when this happens?

Is it implementation dependent? If so, is there any specific method used by most popular compilers?

+82
c ++ exception exception-handling
Jul 07 2018-11-14T00:
source share
5 answers

Yes, the answer depends on the compiler.

A quick experiment with my compiler ( g++ 4.4.3 ) shows that its runtime library first tries to malloc save memory for an exception and, otherwise, tries to allocate space within the "emergency buffer" of the entire system, a data segment. If this does not work, it calls std::terminate() .

It seems that the main task of the emergency buffer is to throw std::bad_alloc after the process std::bad_alloc from the heap space (in this case, the malloc call failed).

Corresponding __cxa_allocate_exception function:

 extern "C" void * __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) throw() { void *ret; thrown_size += sizeof (__cxa_refcounted_exception); ret = malloc (thrown_size); if (! ret) { __gnu_cxx::__scoped_lock sentry(emergency_mutex); bitmask_type used = emergency_used; unsigned int which = 0; if (thrown_size > EMERGENCY_OBJ_SIZE) goto failed; while (used & 1) { used >>= 1; if (++which >= EMERGENCY_OBJ_COUNT) goto failed; } emergency_used |= (bitmask_type)1 << which; ret = &emergency_buffer[which][0]; failed:; if (!ret) std::terminate (); } // We have an uncaught exception as soon as we allocate memory. This // yields uncaught_exception() true during the copy-constructor that // initializes the exception object. See Issue 475. __cxa_eh_globals *globals = __cxa_get_globals (); globals->uncaughtExceptions += 1; memset (ret, 0, sizeof (__cxa_refcounted_exception)); return (void *)((char *)ret + sizeof (__cxa_refcounted_exception)); } 

I do not know how typical this scheme is.

+51
Jul 07 2018-11-11T00:
source share

From this page :

Storage necessary for exceptions thrown. This storage should be kept while the stack is unwound, since it will be used by the handler and be thread safe. An exception object therefore storage will usually be allocated on the heap , although implementations can provide an emergency buffer to support throwing bad_alloc exceptions under low memory conditions.

Now it's just Itanium ABI, and I'm looking for details related to GCC, Clang, and MSVC. However, the standard does not indicate anything, and this seems to be the obvious way to implement an exception store, so ...

+20
Jul 07 2018-11-11T00:
source share

I don't know if this will answer your question, but this (how the C ++ compiler implements exception handling) is a great article on exception handling :. I highly recommend it (:

Sorry for the short answer, but all the information in the article is wonderful, I can’t select and post some information here.

+4
Jul 07 2018-11-11T00:
source share

The C ++ standard usually indicates how the language behaves, and not how the compiler should implement this behavior. I think this question falls into this category. The best way to implement something like this depends on the specifics of the machine - some processors have many general purpose registers, some of them are very few. A processor can even be built with a special register for exceptions only, in which case the compiler should be free to use this function.

0
Jul 07 2018-11-11T00:
source share

Well, it cannot be on the stack, as it will be unwound, and it cannot be on the heap, as this would mean that the system probably could not throw std::bad_alloc . In addition, it is fully consistent with the implementation: an unspecified implementation (which should be documented), but not defined. (An implementation can use the heap most of the time if it has some kind of backup that will limit the number of exceptions, even if there is no more memory.)

-2
Jul 07 2018-11-11T00:
source share



All Articles