Where are objects thrown away?

When I use throw in a function like

 try { // ... throw MyExceptionType() // ... } catch(MyExceptionType& exp){ /* ... */ } 

Where is MyExceptionType allocated? Is it on the stack? If so, is it safe to change exp in my catch ? How about calling some other functions inside catch and using the stack?

In a similar case, I:

 try { char my_array[32]; throw my_array; } catch(char* error_string){ /* ... */ } 

Is error_string pointer to a place in the process stack? Can I run an array if I call some functions inside a catch ?

+4
source share
3 answers

It depends on the implementation. g++ 4.4 tries to keep malloc in memory, and if this failure tries to throw an exception in the system crash buffer, the failure of all that it causes std::terminate fails.

+2
source

This implementation-specific behavior is also strictly related to the ABI used.

If you are on linux / unix, I suggest libsupc++ look at libsupc++ from GNU.

In a nutshell, this, like any other library, you do not have a standard implementation for each standard compiler library / C ++, but you need to look at how this instruction is implemented by those who wrote the library.

+1
source

This is before implementation. Different implementations are different policies, but for the most part they are associated with some special pre-allocated space, static or streaming local. (If you need to highlight the std::bad_alloc , you are in trouble.)

As for your second case, this is not very similar, because an array of pointer conversions occur before the throw. So you're really just throwing a pointer; space for the pointer is processed as usual, but as soon as you left the block where the array was declared, it was gone. Even in the catch (in your example), access to which is undefined behavior.

+1
source

Source: https://habr.com/ru/post/1500303/


All Articles