异常发生后,operator new 会破坏初始化的对象吗?
我有一个类 A
,它使用 new
在其构造函数中分配内存。 当我分配一大块 A
并且(当 new[]
正在初始化单个 A
时)其中一个抛出 std::bad_alloc
在他们的构造函数中?
operator new[]
是否会破坏已经初始化的对象? 或者我有责任确保构造函数不会抛出异常?
编辑: new 的两次调用可能听起来令人困惑,所以这里有一段代码需要澄清:
class A
{
int* mem;
public:
A()
{
try
{
mem = new int[3];
}
catch(bad_alloc&)
{
throw 5;
}
}
~A()
{
delete[] mem;
}
}
A* list = 0;
try
{
list = new A[50000];
}
catch(int)
{
// When I get here, did the new[] above call the destructors
// of all the objects it managed to construct before one of them threw?
}
I have a class A
that allocates memory in its constructor using new
.
What happens when I allocate a chunk of A
s and (while new[]
is initializing the single A
s) one of them throws std::bad_alloc
in their constructor?
Does operator new[]
destruct the already initialized objects?
Or is it my duty to make sure the constructor doesn't throw?
EDIT: The two invocations of new might sound confusing, so here's a piece of code to clarify:
class A
{
int* mem;
public:
A()
{
try
{
mem = new int[3];
}
catch(bad_alloc&)
{
throw 5;
}
}
~A()
{
delete[] mem;
}
}
A* list = 0;
try
{
list = new A[50000];
}
catch(int)
{
// When I get here, did the new[] above call the destructors
// of all the objects it managed to construct before one of them threw?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果执行
new Foo()
并且Foo
的构造函数抛出异常,那么相关operator new
分配的内存将被释放。如果 Foo 任何成员的构造函数成功,则将调用该成员的析构函数。如果执行
new Foo[100]
,并且第 42 个构造函数抛出异常,则所有已构造的 Foo 对象都会被销毁(按相反顺序)。If you do
new Foo()
and the constructor ofFoo
throws, then the memory allocated by the relevantoperator new
will be freed. And if the constructor for any member of Foo has succeeded, that member's destructor will be called.If you do
new Foo[100]
, and the 42nd constructor throws, then all the already-constructed Foo objects are destroyed (in reverse order).是的,任何已分配的对象都将被删除。为什么不自己尝试一下呢?
但是,构造函数抛出的实例的析构函数不会被调用(如果运行上面的代码,您可以看到)。尽管如此,此时构建的成员仍将被删除。例如,如果此构造函数抛出:
i
、b
和f
将被删除,但分配的内存不会被释放(>delete b
不会被调用),并且你会遇到内存泄漏(使用智能指针可以轻松解决这个问题)。有关详细信息,请参阅 C++FaqLite:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.10
http://www.parashift.com/c++-faq-lite/exceptions .html#faq-17.8
Yes, any objects already allocated will be deleted. Why not try it yourself?
However, the destructor of the instance which constructor threw will NOT be called (you can see that if you run the code above). Still, the members which were constructed by that point will be deleted. For example, if this constructor throws:
i
,b
andf
will be deleted, however memory that was allocated will not be freed (delete b
doesn't get called) and you'll have a memory leak (using smart pointers will easily solve that).Take a look at C++FaqLite for more info about this:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.10
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.8
成员将被消灭。构造函数中的自动持续时间变量将超出范围并被正确销毁。
Members will be destroyed. Automatic-duration variables in the constructor will go out of scope and be destroyed properly.
如果某些字段已使用其构造函数成功初始化,则将自动调用析构函数。
If some fields have been successfully initialized with their constructors, the destructors will be called automatically.