操作符重载内存泄漏 [英] operator overloading memory leak

查看:121
本文介绍了操作符重载内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我有一个任务要做在C + +,实现一个Set类与联合,交集等作为重载运算符。我有一个重载运算符+()的问题。我决定使用向量和获得一些算法的库函数的优势。问题是我HAD传递给构造函数数组指针和数组大小。这复杂的任务有点...我可以编译它,但在z = a + b操作我遇到某些内存泄漏。任何人都可以解释我做错了什么?

Recently I got a task to do in C++, implement a Set class with union, intersection etc. as overloaded operators. I've got a problem with overloading an operator+(). I decide to use vectors and get the advantage of some algorithm's library functions. The problem is I HAD TO pass to constructor an array pointer and array size. This complicated this task a bit... I can compile it but during the "z=a+b" operation I encounter somekind of memory leak. Can anyone explain me what am I doing wrong?

class Set {
    int number; // array size (can't be changed)
    int *elems; // array pointer (same)

  public:

    Set();
    Set(int, int*); // (can't be changed)
    ~Set();

  friend Set operator+(const Set& X,const Set& Y){
    std::vector<int> v(X.number+Y.number);
    std::vector<int>::iterator it;

    it=std::set_union (X.elems, X.elems+X.number, Y.elems, Y.elems+Y.number, v.begin());
    v.resize(it-v.begin());

    Set Z;
    Z.number=v.size();
    Z.elems=&v[0];  

    return Z;
  }
};

Set::Set(){};
Set::Set(int n, int* array){
    number=n; 
    elems = array = new int[number];

    for(int i=0; i<number; i++) // creating Set
        std::cin >> elems[i];
    std::sort(elems, elems + number);
}

Set::~Set(){
    delete[] elems;
}

int main(){

   int* pointer;
   Set z;
   Set a = Set(5, pointer);
   Set b = Set(2, pointer);
   z=a+b;
}






我添加了复制构造函数和复制assingment,改变了操作符+()作为NathanOliver建议,现在我传递给构造函数静态数组。仍然有内存泄漏和奇怪的是,我得到这个内存泄漏,即使在主要有只有类变量初始化,无关如果与参数或任何建议?我认为cunstructor是有效的。


I added copy constructor and copy assingment, changed the operator+() as NathanOliver advised and now I am passing to constructor static array. Still have memory leak and strange thing is that I got this memory leak even when in main there is only class variable initialization, doesn't matter if with parameters or not... Any suggestions? I think cunstructor is valid.

Set::Set(int n, int* array){
   number = n; 
   elems = array;
   std::sort(elems, elems + number);
}

Set::Set(const Set& s){
   number=s.number;
   elems=s.elems;
}
Set& operator=(const Set& X){

   if(this==&X)
     return *this;
   delete [] elems;
   elems=X.elems;
   number=X.number;
   return *this;

我使用gcc(tdm64-2)4.8.1编译器。

I use gcc (tdm64-2) 4.8.1 compiler.

 解决方案 

推荐答案

friend set operator +(const Set& X,const Set& Y){
std :: vector< int> v(X.number + Y.number);
std :: vector< int> :: iterator it;

it = std :: set_union(X.elems,X.elems + X.number,Y.elems,Y.elems + Y.number,v.begin());
v.resize(it-v.begin());

设置Z;
Z.number = v.size();
Z.elems =& v [0];

return Z;
}

friend Set operator+(const Set& X,const Set& Y){ std::vector<int> v(X.number+Y.number); std::vector<int>::iterator it; it=std::set_union (X.elems, X.elems+X.number, Y.elems, Y.elems+Y.number, v.begin()); v.resize(it-v.begin()); Set Z; Z.number=v.size(); Z.elems=&v[0]; return Z; }

您创建一个向量,修改它,然后设置 elems 以指向向量包含的内容。问题是当向量在函数结束时被销毁时,向量所保存的内存被释放。所以你现在有一个指针指向你不再拥有的内存。试图对它做任何事情是未定义的行为。你可以做的是创建一个新数组,将向量的元素复制到数组中,然后将新数组赋给`elems

You create a vector, modify it and then set the elems to point to what the vector contains. The issue with that is that when the vector is destroyed at the end of the function the memory that the vector held is released. So you now have a pointer pointing to memory you no longer own. Trying to do anything with it is undefined behavior. What you could do is create a new array, copy the elements of the vector into the array and then assign the new array to `elems

Set Z;
Z.number= v.size();
Z.elems= new int[z.number];
for (int i = 0; i < Z.number; i++)
    Z.elems[i] = v[i];

return Z;

其次,您需要为您的类定义一个复制构造函数和赋值运算符。要执行此操作:什么是三项规则?

Secondly you need to define a copy constructor and assignment operator for you class. To do that reference: What is The Rule of Three?

这篇关于操作符重载内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆