如果删除[] p失败会怎么样? [英] What happens if delete[] p fails?

查看:183
本文介绍了如果删除[] p失败会怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个指向动态分配的10个元素数组的指针:

  T * p = new T [10] ; 

稍后,我要发布该数组:

  delete [] p; 

如果其中一个 T 析构函数会发生什么抛出异常?其他元素还会被破坏吗?内存会释放吗?传播异常还是程序执行终止?



同样,当 std :: vector< T> 被销毁,其中一个 T 析构函数抛出?

解决方案

p>我在标准中看不到明确的提示:



只是他们将以相反的创建顺序被调用



5.3.5删除[expr.delete]




6如果delete-expression的操作数的值不是一个空指针值,delete-expression将调用对象的数组的析构函数(如果有的话)或被删除的数组的元素。在数组的情况下,这些元素将按照递减地址的顺序被破坏(即按照与构造函数完成相反的顺序,见12.6.2)。


即使抛出异常,内存释放也将被执行:


7如果delete-expression的操作数的值不是空指针值,则delete-expression将调用释放功能(3.7.4.2)。否则,是否将调用释放功能是未指定的。 [注意:无论对象的析构函数或数组的某些元素是否引发异常,都将调用释放函数。 - 结束笔记]


我在G ++中尝试了以下代码,它显示了没有更多的析构函数被调用例外:

  #include< iostream> 
int id = 0;
class X
{
public:
X(){me = id ++; std :: cout<< C:开始<我< \\\
;}
〜X(){std :: cout<< C:Done<<我< \\\
;
if(me == 5){throw int(1);}
}
private:
int me;
};

int main()
{
try
{
X data [10];
}
catch(...)
{
std :: cout<<< Finished\\\
;
}
}

执行:

 > g ++ de.cpp 
> ./a.out
C:Start0
C:Start1
C:Start2
C:Start3
C:Start4
C:Start5
C:Start6
C:Start7
C:Start8
C:Start9
C:完成9
C:完成8
C:完成7
C:完成6
C:完成5
完成

其中所有这些都回到这个(非常古老的答案):

从析构函数中抛出异常


Suppose I have a pointer to a dynamically allocated array of 10 elements:

T* p = new T[10];

Later, I want to release that array:

delete[] p;

What happens if one of the T destructors throws an exception? Do the other elements still get destructed? Will the memory be released? Will the exception be propagated, or will program execution be terminated?

Similarly, what happens when a std::vector<T> is destroyed and one of the T destructors throws?

解决方案

I can not see it explicitly called out in the standard:

Just that they will be called in reverse order of creation

5.3.5 Delete [expr.delete]

6 If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2).

And that the memory deallocation will be done even if the exception is thrown:

7 If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2). Otherwise, it is unspecified whether the deallocation function will be called. [ Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. — end note ]

I tried the following code in G++ and it shows that that no more destructors get called after the exception:

#include <iostream>
int id = 0;
class X
{
    public:
         X() {   me = id++; std::cout << "C: Start" << me << "\n";}
        ~X() {   std::cout << "C: Done " << me << "\n";
                 if (me == 5) {throw int(1);}
             }
    private:
        int me;
};

int main()
{
    try
    {
        X       data[10];
    }
    catch(...)
    {
        std::cout << "Finished\n";
    }
}

Execute:

> g++ de.cpp
> ./a.out
C: Start0
C: Start1
C: Start2
C: Start3
C: Start4
C: Start5
C: Start6
C: Start7
C: Start8
C: Start9
C: Done 9
C: Done 8
C: Done 7
C: Done 6
C: Done 5
Finished

Which all leads back to this (very old answer):
throwing exceptions out of a destructor

这篇关于如果删除[] p失败会怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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