我如何确保所有指针都被释放?

发布于 2025-01-18 15:43:22 字数 1153 浏览 2 评论 0原文

我有课。让我们称之为房子。注册表中包含各种财产的房屋。我们称此房屋注册表。在此类中,我想在以不同方式分类的不同房屋中添加一个包含指针的向量(也许是按名称和号码)。在此类中,我具有一个创建新房屋对象并以各自的顺序插入的函数。在这样做时,我要留下内存泄漏,因为该程序可能会以多种方式终止,但没有释放存储在向量中的内存。我知道我可以使用智能指针。但是如何以正确的方式实施它们呢?我故意遗漏了灾难,因为其实施相当明显。但是这个问题是专门针对智能指针的。一个很好的答案最好包括他们与比较器的实施。

class HouseRegistry{
    struct House{
    ....
    }
private:
    vector<House*>HousesbyName;
    vector<House*>HousesbyNumber

    bool newHouse(...){
        House *somehouse = new House;
        ....
        HousesbyName.insert(inserter,somehouse);
        HousesbyNumber.insert(inserter2,somehouse);
        return true;
    }
}

我知道一个解决方案可能看起来像是类似的

class HouseRegistry{
    struct House{
        ....
    }
private:
    vector<shared_ptr<House>>HousesbyName;
    vector<shared_ptr<House>>HousesbyNumber

    bool newHouse(...){
        auto somehouse = make_shared<House>();
        ....
        HousesbyName.insert(inserter,somehouse);
        HousesbyNumber.insert(inserter2,somehouse);
        return true;
    }
}

,但是当比较器功能使用两个房屋指针作为参数时,它会破坏诸如二进制搜索之类的功能。在这种情况下,如果寻找房屋的任何现象,后续的比较器功能会是什么样?

I have a class. Let's call it House. Houses of various properties are contained in a registry. Let's call this house registry. Within this class, I want to add a vector containing pointers to different houses sorted in different ways(perhaps by name and number). Within this class, I have a function that creates new House objects and inserts them in the respective order. In doing so I'm leaving memory leaks as the program may terminate in a myriad of ways but doesn't free up memory stored in the vector. I know I can use smart pointers. But how do I implement them in the right way? I'm intentionally leaving out the destructor as its implementation is fairly obvious. But this question is specifically about smart pointers. A great answer would preferably include their implementation with comparators.

class HouseRegistry{
    struct House{
    ....
    }
private:
    vector<House*>HousesbyName;
    vector<House*>HousesbyNumber

    bool newHouse(...){
        House *somehouse = new House;
        ....
        HousesbyName.insert(inserter,somehouse);
        HousesbyNumber.insert(inserter2,somehouse);
        return true;
    }
}

I know a solution might look something like

class HouseRegistry{
    struct House{
        ....
    }
private:
    vector<shared_ptr<House>>HousesbyName;
    vector<shared_ptr<House>>HousesbyNumber

    bool newHouse(...){
        auto somehouse = make_shared<House>();
        ....
        HousesbyName.insert(inserter,somehouse);
        HousesbyNumber.insert(inserter2,somehouse);
        return true;
    }
}

But it breaks for functions like Binary Search when the comparator function uses two House pointers as arguments. What would a subsequent comparator function look like in this case if looking for any preexisting occurrence of a house?

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

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

发布评论

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

评论(2

爱情眠于流年 2025-01-25 15:43:22
#include <vector>
#include <memory>

std::vector<std::shared_ptr<House>> HousesbyName;
std::vector<std::shared_ptr<House>> HousesbyNumber
auto somehouse = std::make_shared<House>();
...
HousesbyName.insert(inserter, somehouse);
HousesbyNumber.insert(inserter2, somehouse);
...
#include <vector>
#include <memory>

std::vector<std::shared_ptr<House>> HousesbyName;
std::vector<std::shared_ptr<House>> HousesbyNumber
auto somehouse = std::make_shared<House>();
...
HousesbyName.insert(inserter, somehouse);
HousesbyNumber.insert(inserter2, somehouse);
...
握住我的手 2025-01-25 15:43:22

如何确保所有指针都被释放?

一般来说:在不再需要每个动态分配后始终释放它们。

一般答案很简单,但遵循它并不容易。在不同的情况下,有一些方法可以让它变得更容易。最简单的方法是首先根本不手动使用动态分配。您的示例并不一定表明需要它。您可以使用 std::vector 来存储对象。或者,您的用例似乎适合多索引容器。该标准没有提供多索引容器模板,但 Boost 提供了。

但如果您确实需要动态分配,避免泄漏的一个简单方法是永远不要使用 newstd::malloc 等,而是使用容器或 std::make_uniquestd::make_shared 并且永远不要调用 std::unique_ptr::release

How do I ensured all pointers are freed?

In general: By always freeing every dynamic allocation after you no longer need them.

The general answer is simple, and following it isn't easy. There are ways to make it easier in different cases. The easiest way is to not use dynamic allocation manually at all in the first place. Your example doesn't necessarily demonstrate a need for it. You could use std::vector<House> to store the objects. Alternatively, your use cases seems appropriate for a multi-index container. The standard doesn't provide a multi-index container template, but Boost does.

But in case where you do need dynamic allocation, a simple way to avoid leaks is to never use new, std::malloc etc. and instead use containers or std::make_unique or std::make_shared and never call std::unique_ptr::release.

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