8

我对 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

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

4

4 回答 4

22

首先,我不相信有泄漏。您是否正确阅读了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

于 2013-04-15T02:43:37.827 回答
6

我想这可能是答案。就 C++ 而言,这里没有泄漏。

于 2013-04-15T02:41:00.557 回答
3

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

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

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

于 2013-04-15T02:45:53.610 回答
-3

我想你需要打电话

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

在删除之前

于 2013-04-15T02:47:55.637 回答