tommyk
tommyk

Reputation: 3307

In which memory area are exception classes instances created?

I could not find the information where are exception class instances created during exception handling ? In which memory area (stack, heap, static storage, etc.) ? I assume it is not on the stack because of stack-unwinding ...

Upvotes: 7

Views: 141

Answers (4)

Tronic
Tronic

Reputation: 10430

Is kitchen quoted from the standard, it is unspecified. Most implementations allocate them from heap, as they need to survive the stack unwinding process, which may remove stack frames (when throwing outside a function) or create new ones (calling destructors etc). GCC uses built-in function __cxa_allocate_exception for allocating the memory.

Upvotes: 1

xian
xian

Reputation: 4703

From the standard:

15.2.4: The memory for the temporary copy of the exception being thrown is allocated in an unspecified way, except as noted in 3.7.3.1.

And 3.7.3.1 says:

3.7.3.1: All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).

Upvotes: 5

mukeshkumar
mukeshkumar

Reputation: 2778

Exceptions are always thrown by value so no problem even if allocated on stack

Upvotes: 0

anon
anon

Reputation:

The answer is "in a mysterious area managed by the compiler" - seriously, the standard doesn't specify where they must be stored.

Upvotes: 3

Related Questions