How are exceptions allocated on the stack caught beyond their scope?

前端 未结 6 1528
小蘑菇
小蘑菇 2020-12-09 08:44

In the following code, the stack-based variable \'ex\' is thrown and caught in a function beyond the scope in which ex was declared. This seems a bit strange to me, since (A

6条回答
  •  情话喂你
    2020-12-09 09:14

    The object is copied into an exception object that survives stack-unwinding. Where the memory for that object comes from is unspecified. For big object, it will probably be malloc'ed, and for smaller objects, the implementation could have a pre-allocated buffer (i could imagine this could be used for a bad_alloc exception).

    The reference ex is then bound to that exception object, which is a temporary (it has no name).

提交回复
热议问题