【问题标题】:How to really delete vectors如何真正删除向量
【发布时间】:2013-04-07 01:44:43
【问题描述】:

我对 C++ 内存管理很陌生,因为与 C 不同,释放所有内存有更多障碍。

我正在尝试成功删除指向任何类型向量的指针(即向量 * 数据)

/** 
 * We test the program in accessing vectors
 * with dynamic storage
**/
#include <iostream>
#include <vector> // you must include this to use vectors

using namespace std;

int main (void){
    // Now let's test the program with new and delete
    // for no memory leaks with vectors (type safe)
    // however, just calling delete is not enough to free all memory
    // at runtime
    vector <int> * test_vect = new vector <int> (10,5);

    // Print out its size
    cout << "The size of the vector is " << test_vect->size()
         << " elements" << endl;

    // run through vector and display each element within boundary
    for (long i = 0; i < (long)test_vect->size(); ++i){
        cout << "Element " << i << ": " << test_vect->at(i) << endl;
    }

    delete test_vect;
    return EXIT_SUCCESS;
}

但是,我在使用 valgrind 后出现内存泄漏

==2301== HEAP SUMMARY:
==2301==     in use at exit: 4,184 bytes in 2 blocks
==2301==   total heap usage: 4 allocs, 2 frees, 4,248 bytes allocated
==2301== 
==2301== LEAK SUMMARY:
==2301==    definitely lost: 0 bytes in 0 blocks
==2301==    indirectly lost: 0 bytes in 0 blocks
==2301==      possibly lost: 0 bytes in 0 blocks
==2301==    still reachable: 4,096 bytes in 1 blocks
==2301==         suppressed: 88 bytes in 1 blocks

如何使用指向向量的指针(即在运行时)消除所有内存泄漏痕迹?

【问题讨论】:

  • vector &lt;int&gt; * test_vect = ... 违背了向量最初存在的目的。
  • 为什么要使用带有向量的指针..?
  • “因为与 C 不同,释放所有内存存在更多障碍。”笏。
  • 他是否在使用指向整数向量的指针时头脑是否正常并不是问题:看似简单的程序正在泄漏内存。让我们试着帮他找出原因,然后再告诉他为什么做一个指向向量的指针是愚蠢的。
  • 我没有看到泄漏。 stackoverflow.com/questions/3840582/…

标签: c++ memory-management vector memory-leaks


【解决方案1】:

首先,我不相信有泄漏。您是否正确阅读了您为该计划发布的Leak Summary?:

==2301== LEAK SUMMARY:
==2301==    definitely lost: 0 bytes in 0 blocks
==2301==    indirectly lost: 0 bytes in 0 blocks
==2301==      possibly lost: 0 bytes in 0 blocks
==2301==    still reachable: 4,096 bytes in 1 blocks
==2301==         suppressed: 88 bytes in 1 blocks

您在 0 个块中丢失了 0 个字节。 valgrind 找不到任何明确的、间接的或可能的泄漏。你的程序很好。

顺便说一句: 你......真的不应该在这种情况下使用newdelete 与那个向量。 C++ 与 C 完全一样,如果您将它们声明为常规变量,则会在堆栈上进出范围。以下代码完全实现了您在问题中所做的事情,而无需使用 new 和 delete:

int main (void){
    // Now let's test the program with new and delete
    // for no memory leaks with vectors (type safe)
    // however, just calling delete is not enough to free all memory
    // at runtime (ThePhD: it is, actually!)
    vector <int>  test_vect(10,5);

    // Print out its size
    cout << "The size of the vector is " << test_vect.size() 
         << " elements" << endl;

    // run through vector and display each element within boundary
    for (long i = 0; i < (long)test_vect.size(); ++i){
        cout << "Element " << i << ": " << test_vect.at(i) << endl;
    }

    // ( ThePhD: Look ma, no deletions! )
    return EXIT_SUCCESS;
}

堆栈会神奇地自行清理,因为 std::vector 有一个析构函数,可以在超出范围时清理其资源。您无需将其动态化并将其放在程序内存的堆/空闲存储区域中,堆栈就可以做到这一点。

另外,听起来你来自 C,所以我会花时间说:欢迎使用 C++。 :D

【讨论】:

  • 虽然您的建议适用于小型和简单的程序,但如果我真的必须为向量创建动态分配的内存,我该怎么办?超出函数调用的向量。
  • @JBRPG 您将向量作为返回值传递(这很快,因为 C++ 现在有std::move 和朋友),或者您动态分配向量并自己跟踪它。您也可以使用std::unique_ptr&lt;std::vector&lt;int&gt;&gt;,这将与指向 std::vector 的指针具有相同的语义,但在传入新向量后会自动删除。你也可以在C++的函数中通过引用传递东西,对它们进行操作,函数存在后的值会直接被修改。现在是 C++ 的好时机。 :D
【解决方案2】:

我认为这就是答案。就 C++ 而言,这里没有泄漏。

【讨论】:

    【解决方案3】:

    这些容器类的全部目的是为您进行内存管理、分配、重新分配和释放。你把容器放到栈上,它内部保持动态分配的内容,当容器实例被删除时,它会自动删除动态分配的数据。

    容器本身的动态实例化违背了目的并且几乎总是完全没有意义。

    是的,在你的情况下,向量真的被删除了。

    【讨论】:

    • 我必须同意你的观点,因为当我搜索其他问题时,大多数帖子都确认 vector 是一个动态内存类包装器,这让我不必手动添加和删除
    • 它不仅仅是向量,在 C++ 标准库中还有许多容器类,在 Qt 或 boost 等库中甚至更多。
    【解决方案4】:

    我觉得你需要打电话

    if(test_vect != 0)
    {
       if(!test_vect->empty())
          test_vect->clear();
    }
    

    删除前

    【讨论】:

    • 没有。这与它无关。
    猜你喜欢
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多