简体   繁体   English

C ++,擦除向量

[英]C++ , Erasing Vector

i'm new in cpp.I defined a vector with this code and in the end , i want to erase it completely. 我是cpp的新手,我用此代码定义了一个向量,最后我想完全删除它。

#include <string>
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;


struct MCLASS
{
    string *a = new string();
    string *b = new string();

    MCLASS(string ax,string bx)
    {
        a = new string(ax);
        b = new string(bx);
    }

    ~MCLASS()
    {
        delete a;
        delete b;
    }
};

vector<MCLASS*> *vc = new vector<MCLASS*>;




int main()
{
    for (int i = 0 ; i <= 1000000 ; i++)
        vc->push_back(new MCLASS("EHEM","UHUM"));

    for (int i = 0 ; i <= 1000000 ; i++)
        delete vc->at(i);

    vc->clear();
    delete vc;
    cout<<"Deleted";
    cin.get();

    return 0;
}

but there is a problem.I can't erase it completely.I tried to delete each items of vector , clear vector items , and delete it.but when i check the RAM Usage of this application , it uses about 130MB.Please explain to me what to do.Thanks! 但是有一个问题。我无法完全擦除它。我试图删除矢量的每个项目,清除矢量项目,然后将其删除。但是当我检查此应用程序的RAM使用情况时,它使用了约130MB。请向我该怎么办。谢谢!

You're calling new string twice per member variable. 您正在为每个成员变量调用两次new string For each one, the result of the second call to new string overwrites the result of the first, so the first never gets delete d and there's your memory leak. 对于每个new string ,第二次调用new string的结果都会覆盖第一个new string的结果,因此第一个永远不会被delete d,这会导致内存泄漏。

Don't call new string in the declaration of a and b —only in the constructor. 不要在ab的声明中调用new string仅在构造函数中。

struct MCLASS
{
    string *a;
    string *b;
...

From cplusplus.com's article on vector::clear : 来自cplusplus.com关于vector::clear文章

A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. 由于调用此函数,不能保证会发生重新分配,也不保证矢量容量会发生变化。 A typical alternative that forces a reallocation is to use swap: vector<T>().swap(x); // clear x reallocating 强制重新分配的典型替代方法是使用swap: vector<T>().swap(x); // clear x reallocating vector<T>().swap(x); // clear x reallocating

As VladimirS suggested in his comment; 正如弗拉基米尔(VladimirS)在评论中所建议的那样; memory management is not a simple beast, however the above should reduce your memory consumption provided nothing has prevented that by replacing delete (such as some memory inspection tools). 内存管理不是一个简单的野兽,但是上述操作应该减少您的内存消耗,前提是没有任何事情可以通过替换Delete来阻止它(例如某些内存检查工具)。

If you want to erase all contents of a std::vector you can either call the clear member function or resize it to zero size. 如果要擦除std::vector所有内容,则可以调用clear成员函数或将其大小调整为零。

There's a slight difference in result. 结果略有不同。 clear will destroy all elements but (likely) preserve the vectors capacity. clear将破坏所有元素,但(可能)保留向量的容量。 resize(0) will destroy all elements and also reduce capacity to zero. resize(0)将破坏所有元素,并将容量减少到零。

Your error takes place on lines 10-11 : 您的错误发生在第10-11行:

string *a = new string();
string *b = new string();

Indeed, you create two pointers a and b and you allocate memory for them, memory you'll never free because you re-allocate new memory in the constructor without checking values. 确实,您创建了两个指针ab并为它们分配了内存,因为您在不检查值的情况下在构造函数中重新分配了新内存,所以您将永远无法释放内存。

Proper way to fix that (and good practice) is to initialize pointers attributes to NULL . 解决该问题(正确的做法)的正确方法是将指针属性初始化为NULL In your case replace these lines like that: 在您的情况下,请按以下方式替换这些行:

string *a = NULL;
string *b = NULL;

then, allocate it in constructor or somewhere... 然后,将其分配在构造函数中或某个地方...

Tips : 温馨提示

  • Before a new or a delete , check current values ;) 在进行新操作删除操作之前,请检查当前值;)
  • Use Valgrind or a tool like that to check memory leak 使用Valgrind或类似工具检查内存泄漏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM