Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 793
dasfex Avatar asked Dec 11 '25 21:12

dasfex


1 Answers

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]. ...

like image 115
R Sahu Avatar answered Dec 13 '25 10:12

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!