0

您好,我正在尝试删除班级中的动态内存,但出现以下错误:

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

*在 malloc_error_break 中设置断点以调试 Abort 陷阱: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;

}

4

2 回答 2

4

您有一个默认构造函数(有一个错误)和赋值运算符,但您还需要定义一个复制构造函数。如果不这样做,则 a 的每个副本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您拥有多少 s,因此我建议您这样做。

您还需要修复默认构造函数;至少设置polynullptr这样你delete[]的析构函数中就不会出现未初始化的指针。

于 2012-09-27T14:55:10.847 回答
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
};

您还应该适当地实现移动 ctor 和运算符,以便polynomial sum = a+b;在复制临时文件时不需要重新分配内存等表达式。

于 2012-09-27T14:57:24.210 回答