在智能指针中存储数组类型时是否明确需要提供删除器?

发布于 2025-01-12 15:30:40 字数 1440 浏览 3 评论 0原文

根据这篇论文,常见的之一开发人员犯的错误(#3)是使用具有数组类型的智能指针;主要是因为会调用操作符delete而不是delete[],导致程序出现内存泄漏。

尽管如此;查找C++参考中的默认删除器,有以下代码

#include <memory>
#include <vector>
#include <algorithm>
 
int main()
{
//  {
//      std::shared_ptr<int> shared_bad(new int[10]);
//  } // the destructor calls delete, undefined behavior
 
    {
        std::shared_ptr<int> shared_good(new int[10], std::default_delete<int[]>());
    } // the destructor calls delete[], ok
 
    {
        std::unique_ptr<int> ptr(new int(5));
    } // unique_ptr<int> uses default_delete<int>
 
    {
        std::unique_ptr<int[]> ptr(new int[10]);
    } // unique_ptr<int[]> uses default_delete<int[]>
 
    // default_delete can be used anywhere a delete functor is needed
    std::vector<int*> v;
    for(int n = 0; n < 100; ++n)
        v.push_back(new int(n));
    std::for_each(v.begin(), v.end(), std::default_delete<int>());
}

:直接与此相矛盾;但这可能是因为 C++ 标准不同;情况确实如此,还是论文中的#3 根本不真实?

使用带有智能指针的数组时是否需要显式提供删除器,或者它是否依赖于 C++ 标准(例如 C++11 或 c++17; c++20 中的不同行为);或平台(例如 gcc、MSVC 等)?

According to this paper, one of the common mistakes developers make ( #3 ) is using a smart pointer with array types; Mainly because the operator delete will be called instead of delete[], leaving the program with a memory leak.

Depite that; looking up default deleter in the C++ reference, the following code is there:

#include <memory>
#include <vector>
#include <algorithm>
 
int main()
{
//  {
//      std::shared_ptr<int> shared_bad(new int[10]);
//  } // the destructor calls delete, undefined behavior
 
    {
        std::shared_ptr<int> shared_good(new int[10], std::default_delete<int[]>());
    } // the destructor calls delete[], ok
 
    {
        std::unique_ptr<int> ptr(new int(5));
    } // unique_ptr<int> uses default_delete<int>
 
    {
        std::unique_ptr<int[]> ptr(new int[10]);
    } // unique_ptr<int[]> uses default_delete<int[]>
 
    // default_delete can be used anywhere a delete functor is needed
    std::vector<int*> v;
    for(int n = 0; n < 100; ++n)
        v.push_back(new int(n));
    std::for_each(v.begin(), v.end(), std::default_delete<int>());
}

Which directly contradicts that; But this might be because of different C++ standards; Is this the case, or is #3 in the paper simply untrue?

Does a deleter needs to be provided explicitly when using arrays with smart pointers, or does it depend on the c++ standard ( e.g. different behaviour in C++11 or c++17; c++20 ); or the platform (e.g. gcc, MSVC etc.. )?

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

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

发布评论

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

评论(1

平生欢 2025-01-19 15:30:40

如果我理解正确的话,你似乎认为示例4与论文相矛盾,论文建议使用示例2。这不是论文所说的。

示例 1 是未定义的行为,如论文中所述:“对数组使用智能指针,例如 auto_ptr、unique_ptr、shared_ptr 也是不正确的。”

示例 2 工作正常,但是论文中根本没有提到使用自定义删除器。

示例 3 创建指向单个 int 的指针。这与论文无关。

示例 4 创建了指向 int[] 的指针,论文中提到这是一种有效用法:“如果数组需要使用智能指针,则可以使用 --a unique_ptr< T[]> 专业化。”

If I understand correctly, you seem to think that the example 4 contradicts the paper, and the paper recommends using example 2. This is not what the paper is saying.

Example 1 is undefined behavior, as described in the paper: "Using smart pointers, such as auto_ptr, unique_ptr, shared_ptr, with arrays is also incorrect."

Example 2 works correctly, however using a custom deleter is not mentioned in the paper at all.

Example 3 creates pointer to single int. This is not related to the paper.

Example 4 creates pointer to int[], which is mentioned in the paper as a valid usage: "If using of a smart pointer is required for an array, it is possible to use -- a unique_ptr<T[]> specialization."

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