是 C++容器异常安全?

发布于 2024-11-19 05:43:28 字数 469 浏览 2 评论 0原文

我使用隐式构造函数和一个输入属性的 load() 成员,但也可以抛出异常。

我的问题是:如果属性是日常 C++ 容器,如果 load() 中发生异常,我是否会出现内存泄漏?

感谢您的阅读。

编辑:示例代码有助于澄清我的问题。

class One
{
public:
    std::vector<int> stuff;

    void load() {
        stuff.resize(13);
        stuff[0] = 43;

        std::bad_alloc ba;
        throw ba; // will this cause memory leaks? (as far as this class is concerned)
    }
}

我知道这是一个愚蠢的问题,但我不得不问。

I use the implicit Constructor and a load() Member which inputs the Attributes, but can also throw exceptions.

My question is: if the Attributes are everyday C++ Containers will I get memory leaks if exceptions happen in load()?

Thank you for reading.

Edit: example code to help clarify my question.

class One
{
public:
    std::vector<int> stuff;

    void load() {
        stuff.resize(13);
        stuff[0] = 43;

        std::bad_alloc ba;
        throw ba; // will this cause memory leaks? (as far as this class is concerned)
    }
}

I know this is a silly question but I had to ask it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

枕头说它不想醒 2024-11-26 05:43:28

容器本身是异常安全的。
但这也取决于放置在容器中的类型以及是否正确编写。

即:异常不应逃逸析构函数

该标准定义了对容器和异常的以下保证:

23.2.1 一般容器要求 [container.requirements.general]

第 10 段:

除非另有规定(参见 23.2.4.1、23.2.5.1、23.3.3.4 和 23.3.6.5),本条款中定义的所有容器类型均满足以下附加要求:
— 如果在插入单个元素时 insert() 函数抛出异常,则该函数不起作用。
— 如果push_back() 或push_front() 函数抛出异常,则该函数没有任何效果。
— 没有擦除(),清除(),pop_back()或pop_front()函数抛出异常。
— 返回的迭代器的复制构造函数或赋值运算符不会引发异常。
— 没有 swap() 函数抛出异常。

The container itself is exception safe.
But it also depends on the type being placed in the contain and if it has been written correctly.

i.e.: Exceptions should not escape the destructor

The standard defines the following guarantees on containers and exceptions:

23.2.1 General container requirements [container.requirements.general]

Paragraph 10:

Unless otherwise specified (see 23.2.4.1, 23.2.5.1, 23.3.3.4, and 23.3.6.5) all container types defined in this Clause meet the following additional requirements:
— if an exception is thrown by an insert() function while inserting a single element, that function has no effects.
— if an exception is thrown by a push_back() or push_front() function, that function has no effects.
— no erase(), clear(), pop_back() or pop_front() function throws an exception.
— no copy constructor or assignment operator of a returned iterator throws an exception.
— no swap() function throws an exception.

千と千尋 2024-11-26 05:43:28

是的,容器是异常安全的。只要你不做一些恶作剧,比如在堆上分配它们(没有异常安全的智能指针)或类似的事情,你就会没事的。

Yes, containers are exception safe. As long as you aren't doing shenanigans like allocating them on the heap (without exception-safe smart pointers) or such like that, you'll be fine.

甚是思念 2024-11-26 05:43:28

由于你的问题没有说明太多,这是我的看法。

如果您使用 new/new[] (在 load() 内部)分配内存,那么您必须使用 delete/delete[] 取消分配,当抛出异常时。

如果您作为自动变量进行分配,那么它们是异常安全的。

Since your question doesn't state much, here is my take.

If you are allocating memory using new/new[] (inside your load()) then you have to deallocate using delete/delete[], when exception is thrown.

If you are allocating as an automatic variable then they are exception safe.

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