被释放的c ++指针没有被分配

时间:2012-09-27 14:40:31

标签: c++ memory dynamic operator-overloading

您好我试图删除我班级中的动态内存,但是我收到以下错误:

a.out(2830) malloc: *** error for object 0x7fb38a4039c0: pointer being freed was not allocated

* 在malloc_error_break中设置断点以进行调试 中止陷阱:6

我认为这与我的重载" ="有关。操作员不能直接访问我的班级"&"。

class Polynomial{
    public:
        //Constructors / Destructors
        Polynomial();
        Polynomial(int tempNum, int * tempPoly);
        ~Polynomial();

        //Member Functions & Operator overloading
        //Addition
        Polynomial operator+(Polynomial& Poly);
        //Subtraction
        Polynomial operator-(Polynomial& Poly);
        //Multiplication
        Polynomial operator*(Polynomial& Poly);
        //Assignment
        Polynomial operator=(Polynomial Poly);

        //Insertion
        friend ostream& operator<<(ostream& os, Polynomial& Poly);
        //Extraction
        friend istream& operator>>(istream& is, Polynomial& Poly);

    private:
        //Data Members
        //Int array (degree of polynomial + 1)
        int * poly;
        int polyNum;
};


Implementation: 

Polynomial::Polynomial(){
    polyNum = 0;
}

Polynomial::~Polynomial(){
    //Delete Dynamic Memory
    delete [] poly;
}

Polynomial::Polynomial(int tempNum, int *tempPoly){

    poly = new int[tempNum+1];

    polyNum = tempNum;

    for (int i=0; i < tempNum; i++) {
        poly[i] = tempPoly[i];
    }
}

//Addition
Polynomial Polynomial::operator+(Polynomial &Poly){
    Polynomial temp;

    if(polyNum > Poly.polyNum){
        temp.polyNum = polyNum;
    }
    else{
        temp.polyNum = Poly.polyNum;
    }

    temp.poly = new int[temp.polyNum + 1];

    for(int i=0; i < temp.polyNum; i++){
        temp.poly[i] = Poly.poly[i] + poly[i];
    }

    return (temp);
}

//Subtraction
Polynomial Polynomial::operator-(Polynomial& Poly){
    Polynomial temp;

    if(polyNum > Poly.polyNum){
        temp.polyNum = polyNum;
    }
    else{
        temp.polyNum = Poly.polyNum;
    }

    temp.poly = new int[temp.polyNum + 1];

    for(int i=0; i < temp.polyNum; i++){
        temp.poly[i] = poly[i] - Poly.poly[i];
    }

    return (temp);
}

//Multiplication
Polynomial Polynomial::operator*(Polynomial& Poly){

    Polynomial temp;

    //make coefficient array
    temp.polyNum = (polyNum + Poly.polyNum) - 1;

    temp.poly = new int [temp.polyNum];

    for (int i = 0; i < temp.polyNum; i++)
    {
        for (int j = 0; j < Poly.polyNum; j++){
            temp.poly[i+j] += poly[i] * Poly.poly[j];
        }
    }

    return temp;
}

//Assignment
Polynomial Polynomial::operator=(Polynomial Poly){

    polyNum = Poly.polyNum;

    poly = new int[polyNum+1];

    for (int i=0; i < polyNum; i++) {
        poly[i] = Poly.poly[i];
    }

    return *this;
}

//Insertion
ostream& operator<<(ostream& os, Polynomial& Poly){

    for (int i=0; i < Poly.polyNum; i++) {
        os << Poly.poly[i] << " x^" << i;

        if(i != Poly.polyNum - 1){
            os << " + ";
        }
    }

    return os;
}

//Extraction
istream& operator>>(istream& is, Polynomial& Poly){

    int numP = 0;
    int * tempP;

    is >> numP;

    tempP = new int [numP+1];

    for (int i=0; i < numP; i++) {
        is >> tempP[i];
    }

    Poly.polyNum = numP;

    Poly.poly = new int[Poly.polyNum +1];

    for (int i=0; i < Poly.polyNum; i++) {
        Poly.poly[i] = tempP[i];
    }

    delete [] tempP;

    return is;
}

主要

int main(){

// Input Polynomial #1 (P1)
cout << "Input polynominal p1: " << endl;
Polynomial P1;
cin >> P1;

// Output Polynominal
cout << "p1(x) = " << P1 << '\n' << endl;

// Input Polynomial #2 (P2)
cout << "Input polynominal p2: " << endl;
Polynomial P2;
cin >> P2;

// Output Polynominal
cout << "p2(x) = " << P2 << '\n' << endl;

// Copy P2 to P3 and output P3
Polynomial P3;
P3 = P2;
cout << "Copy p2 to p3, p3(x) = " << P3 << '\n' << endl;

// Add P1 to P2 and output to P3
Polynomial P4;
P4 = P1 + P2;
cout << "p3(x) = p1(x) + p2(x) = " << P4 << '\n' << endl;

// Subtract P1 from P2 and output to P3
Polynomial P5;
P5 = P1 - P2;
cout << "p3(x) = p1(x) - p2(x) = " << P5 << '\n' << endl;

// Subtract P2 from P1 and output to P3
Polynomial P6;
P6 = P2 - P1;
cout << "p3(x) = p2(x) - p1(x) = " << P6 << '\n' << endl;

// Multiply P1 by P2 and output to P3
Polynomial P7;
P7 = P1 * P2;
cout << "p3(x) = p1(x) * p2(x) = " << P7 << '\n' << endl;

return 0;

}

2 个答案:

答案 0 :(得分:4)

您有一个默认构造函数(有一个bug)和赋值运算符,但您还需要定义一个复制构造函数。如果不这样做,Polynomial的每个副本都会获得与其复制的poly相同的指针,并且每次销毁其中一个副本时都会释放相同的指针。第二次发生这种情况时,您会在已经delete[]的指针上调用delete[],从而导致此错误。

您的复制构造函数应该看起来像这样

Polynomial::Polynomial(const Polynomial& rhs) : poly(new int[rhs.polynum]), polynum(rhs.polynum) {
    std::copy(rhs.poly, rhs.poly + polynum, poly);
}

也就是说,如果你使用std::vector,你就不必拥有赋值运算符或复制构造函数,甚至不需要单独的变量来跟踪你有多少poly个,所以我建议这样做。

您还需要修复默认构造函数;至少将poly设置为nullptr,这样您就不会在析构函数中delete[]使用未初始化的指针。

答案 1 :(得分:4)

Polynomial::Polynomial(){
    polyNum = 0;
}
Polynomial::~Polynomial(){
    //Delete Dynamic Memory
    delete [] poly;           // <-- bug
}

这是一个错误:poly未在默认ctor中初始化,但在dtor中删除。

修改

你应该使用std::vector<int>

class polynomial
{
  std::vector<int> _pol;       
public:
  size_t degree() const { return _pol.empty()? 0 : _pol.size() - 1; }
  polynomial() = default;                   // could be omitted (compiler generated anyway)
  polynomial(polynomial const&) = default;  // ----
  /// evaluate polynomial
  template<typename T>
  T operator() (T const&x) const
  {
    T y(0);
    T p(1);
    for(auto i : _pol) {
      y += *i * p;
      p *= x;
    }
    return y;
  }
  polynomial& operator+=(polynomial const&other)
  {
    auto j = other._pol.begin();
    for(auto i = _pol.begin(); i!=_pol.end() && j!=other._pol.end(); ++i,++j)
      *i += *j;
    for(; j != other._pol.end(); ++j)
      _pol.push_back(*j);
    return*this;
  }
  polynomial operator+(polynomial const&other) const
  {
    polynomial _p(*this);
    return _p += other;           
  }
  // etc
};

你还应该适当地实现move ctor和operator,这样在polynomial sum = a+b;这样的表达式中,复制临时文件时不需要重新分配内存。