在构造"新"对象期间发生异常时,堆分配的内存会发生什么?

Cra*_*aig 3 c++ memory

当一个类在构造期间抛出异常时,分配的内存会发生什么,你将如何处理这样的事情.例如:

std::auto_ptr<ThirdPartyClass> au_tpc;

try
{
    au_tpc.reset(new ThirdPartyClass());
}
catch(...)
{
    // What happened to the memory allocated of 
    // sizeof(ThirdPartyClass) for the new instance?
}
Run Code Online (Sandbox Code Playgroud)

R. *_*des 7

它只是有效.在执行异常处理块之前,将释放内存.

  • @JoachimPileborg有什么记忆?如果构造函数抛出,则不会调用`reset`。 (2认同)