Reputation: 1270
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
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 returningT
” to “pointer toT
” or “pointer to function returningT
”, 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