异常发生后,operator new 会破坏初始化的对象吗?

发布于 2024-12-19 12:29:16 字数 775 浏览 1 评论 0原文

我有一个类 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 As and (while new[] is initializing the single As) 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〆凄凉。 2024-12-26 12:29:16

如果执行 new Foo() 并且 Foo 的构造函数抛出异常,那么相关operator new 分配的内存将被释放。如果 Foo 任何成员的构造函数成功,则将调用该成员的析构函数。

如果执行 new Foo[100],并且第 42 个构造函数抛出异常,则所有已构造的 Foo 对象都会被销毁(按相反顺序)。

If you do new Foo() and the constructor of Foo throws, then the memory allocated by the relevant operator 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).

眼藏柔 2024-12-26 12:29:16

是的,任何已分配的对象都将被删除。为什么不自己尝试一下呢?

#include <iostream>
#include <stdexcept>

class A {
    static int n;
public:
    A()
    {
        ++n;
        std::cout << "construncting A no. " << n << "...";
        // let's pretend we allocate something here
        // with new and it fails
        if (n == 5) {
            std::cout << std::endl;
            throw std::bad_alloc();
        }
        std::cout << "done\n";
    }
    ~A()
    {
        --n;
        std::cout << "destructed A no. " << n << "\n";
    }
};

int A::n = 0;

int main()
{
    try {
        A* a = new A[10];
    }
    catch (std::bad_alloc& e) {
        std::cout << "caught an exception!\n";
    }
}

但是,构造函数抛出的实例的析构函数不会被调用(如果运行上面的代码,您可以看到)。尽管如此,此时构建的成员仍将被删除。例如,如果此构造函数抛出:

class foo {
    int i;
    bar* b;
public:
    foo() : i(42), b(new bar)
    {
        float f = 3.14;
        // something throws an exception here
    }
    ~foo()
    {
        delete b;
    }
};

ibf 将被删除,但分配的内存不会被释放(>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?

#include <iostream>
#include <stdexcept>

class A {
    static int n;
public:
    A()
    {
        ++n;
        std::cout << "construncting A no. " << n << "...";
        // let's pretend we allocate something here
        // with new and it fails
        if (n == 5) {
            std::cout << std::endl;
            throw std::bad_alloc();
        }
        std::cout << "done\n";
    }
    ~A()
    {
        --n;
        std::cout << "destructed A no. " << n << "\n";
    }
};

int A::n = 0;

int main()
{
    try {
        A* a = new A[10];
    }
    catch (std::bad_alloc& e) {
        std::cout << "caught an exception!\n";
    }
}

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:

class foo {
    int i;
    bar* b;
public:
    foo() : i(42), b(new bar)
    {
        float f = 3.14;
        // something throws an exception here
    }
    ~foo()
    {
        delete b;
    }
};

i,b and f 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

给不了的爱 2024-12-26 12:29:16

成员将被消灭。构造函数中的自动持续时间变量将超出范围并被正确销毁。

Members will be destroyed. Automatic-duration variables in the constructor will go out of scope and be destroyed properly.

木落 2024-12-26 12:29:16

如果某些字段已使用其构造函数成功初始化,则将自动调用析构函数。

If some fields have been successfully initialized with their constructors, the destructors will be called automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文