dasfex
dasfex

Reputation: 1270

Where are program allocate memory for throwing object?

If I throw some string from function h()

f()->g()->h()

in this way

throw std::string("error");

and catch only in f(). Where are program allocate memory for this string? Obviously it can't allocate it on the stack of h(). If this is stack of f() in what way it know in what place it should allocate?

Upvotes: 3

Views: 68

Answers (1)

R Sahu
R Sahu

Reputation: 206607

If this is stack of f() in what way it know in what place it should allocate?

It's a temporary object allocated in an unspecified way.

From the Throwing an exception section of the standard:

3 A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from “array of T” or “function returning T” to “pointer to T” or “pointer to function returning T”, respectively. ...

and

4 The memory for the exception object is allocated in an unspecified way, except as noted in [basic.stc.dynamic.allocation]. ...

Upvotes: 2

Related Questions