异常对象在哪里有其空间,堆或堆栈,以及如何在不同的类中访问它? [英] Where does exception object have its space, heap or stack, and how to access it in different class?

查看:34
本文介绍了异常对象在哪里有其空间,堆或堆栈,以及如何在不同的类中访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,一名面试官问我 C ++中的异常对象在哪里分配,堆或堆栈?我不确定,但是我回答了堆栈,因为我认为没有"new"或"malloc" .正确吗?

Recently an interviewer asked me where the exception object in C++ is allocated, heap or stack? I'm not sure but I answered stack since I thought there is no "new" or "malloc". Is it correct?

然后他一直问我如果它在堆栈上,假设A类抛出异常对象,我们说"e",而B类捕获"e".由于"e"在A的堆栈中,那么B如何访问该"e"?

我对第二个问题不太清楚.谁能给我一些示例代码,说明"A类抛出e而B类捕获了它"?另外,我猜想B可以通过复制值或地址来捕获e,但是访问者只是拒绝我的答案而没有给我一个正确的答案,那么正确的答案是什么,是否有任何机制可以确保类对象可以捕获其他类对象的异常?谢谢〜

I'm not very clear about the second question. Could anyone can give me some example code showing that "class A throw e and class B catch it"? Also, I guessed B can catch e by copying the value or address but the interviewer only denied my answer without giving me right one, so what is the right answer, is there any mechanism can ensure class object can catch exceptions from other class objects? Thanks~

推荐答案

来自[except.throw]/15.1/4:

From [except.throw]/15.1/4:

除3.7.4.1所述外,用于异常对象的内存是以未指定的方式分配的.

The memory for the exception object is allocated in an unspecified way, except as noted in 3.7.4.1.

最终参考文献[basic.stc.dynamic.allocation]/4说:

The final reference, [basic.stc.dynamic.allocation]/4, says:

[注意:特别是,不会调用全局分配函数来为异常对象(15.1)分配存储. — 尾注]

[Note: In particular, a global allocation function is not called to allocate storage for [...] an exception object (15.1). — end note]

这篇关于异常对象在哪里有其空间,堆或堆栈,以及如何在不同的类中访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆