0

I am trying to build a dynamic array in C++ to work with a polynomial class for a project. I am pretty new to C++ and I am pretty lost. I believe I have allocated the memory properly, but I am running into an issue with my destructor, saying "the memory being freed was not allocated." If I comment it out, it works, but I am lost after that. Any thoughts?

#ifndef __Chapter9Program__Polynomial__
#define __Chapter9Program__Polynomial__

#include <iostream>
using namespace std;

class Polynomial
{
    public:
    Polynomial(int deg, int coeff[]);
    Polynomial operator+(Polynomial other);
    Polynomial operator-(Polynomial other);
    Polynomial operator*(Polynomial other);
    ostream& operator<<(ostream& os);//, const Polynomial& object);
    const int getDegree();
    const int getCoeff(const int index);
    double evaluateAt(double x); //Finds P(x)
    void show();
    string print();
    // destructor
    ~Polynomial();
private:
    int degree;
    int *coefficient; //Alllocate memory
};

#endif /* defined(__Chapter9Program__Polynomial__) */


#include "Polynomial.h"
using namespace std;
#include <iostream>

//Constructor
Polynomial::Polynomial(int deg, int coeff[])
{
degree = deg;
coefficient = new int [degree];
for( int i = 0; i <= degree; i++)
{
    coefficient[i] = coeff[i];
}
}

//Destructor
Polynomial::~Polynomial()
{
if( coefficient )
{
    delete [] coefficient;
    coefficient = NULL;
}
}

//finds P(x)
double Polynomial::evaluateAt(double x)
{
double sum = 0.0;
double xPow = 1.0;
if( coefficient )
    for(int i=0; i<degree; i++)
    {
        sum += xPow*coefficient[i];
        xPow *= x;
    }

return sum;
}

Polynomial Polynomial::operator+(Polynomial other)
{
int first[degree];
int second[degree];
for(int i = 0; i< degree; i++)
{
    first[i] = 0;
    second[i] = 0;
}
int newDeg;
int temp[degree];
int final[degree];
int final2[degree];
if(degree > other.getDegree())
{
    newDeg = degree;
}
else
{
    newDeg = other.getDegree();
}
if(degree > other.getDegree())
{
    for(int i=0;i<=degree;i++)
    {
        first[i] = coefficient[degree-i];
    }
    for(int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(other.getDegree()-i);
    }
    for(int i = 0; i<degree; i++)
    {
        temp[i] = first[i]+second[i];
    }
    for(int i=degree-1; i>=0; i--)
    {
        final[i] = temp[i-i];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree-1)-(newDeg-i)];
    }
}
else if(degree == other.getDegree())
{
    for(int i=0;i<=degree;i++)
    {
        first[i] = coefficient[i];
    }
    for( int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(i);
    }
    for(int i = 0; i<degree; i++)
    {
        final[i] = first[i]+second[i];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree - 1)-(newDeg-i)];
    }
}
else
{
    for( int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(other.getDegree()-i);
    }
    for(int i = 0; i <= degree; i++)
    {
        first[i] = coefficient[degree-i];
    }
    for(int i = 0; i<degree; i++)
    {
        temp[i] = first[i]+second[i];
    }
    for(int i=degree-1; i>=0; i--)
    {
        final[i] = temp[i-1];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree - 1)-(newDeg-i)];
    }
}
return Polynomial(newDeg,final2);
}

Polynomial Polynomial::operator-(Polynomial other)
{
int first[degree];
int second[degree];
for(int i = 0; i< degree; i++)
{
    first[i] = 0;
    second[i] = 0;
}
int newDeg;
int temp[degree];
int final[degree];
int final2[degree];
if(degree > other.getDegree())
{
    newDeg = degree;
}
else
{
    newDeg = other.getDegree();
}
if(degree > other.getDegree())
{
    for(int i=0;i<=degree;i++)
    {
        first[i] = coefficient[degree-i];
    }
    for(int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(other.getDegree()-i);
    }
    for(int i = 0; i<degree; i++)
    {
        temp[i] = first[i]-second[i];
    }
    for(int i=0; i<degree; i++)
    {
        final[i] = temp[(degree-1)-i];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree - 1)-(newDeg-i)];
    }
}
else if(degree == other.getDegree())
{
    for(int i=0;i<=degree;i++)
    {
        first[i] = coefficient[i];
    }
    for( int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(i);
    }
    for(int i = 0; i<degree; i++)
    {
        final[i] = first[i]-second[i];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree - 1)-(newDeg-i)];
    }
}
else
{
    for( int i = 0; i <= other.getDegree(); i++)
    {
        second[i] = other.getCoeff(other.getDegree()-i);
    }
    for(int i = 0; i <= degree; i++)
    {
        first[i] = coefficient[degree-i];
    }
    for(int i = 0; i<degree; i++)
    {
        temp[i] = first[i]-second[i];
    }
    for(int i=0; i<degree; i++)
    {
        final[i] = temp[(degree - 1)-i];
    }
    for(int i=0;i<=newDeg;i++)
    {
        final2[i] = final[(degree - 1)-(newDeg-i)];
        //cout<<final2[i]<<endl;
    }
}
return Polynomial(newDeg,final2);
}

Polynomial Polynomial::operator*(Polynomial other)
{
int newDeg = degree + other.getDegree();
int resultCoeff[degree];
for(int i = 0; i<degree; i++)
{
    resultCoeff[i] = 0;
}
//cout<<resultCoeff[5]<<endl;
for(int i = 0; i<=degree; i++)
{
    for(int j = 0; j<=other.getDegree(); j++)
    {
        resultCoeff[i+j] += (other.getCoeff(j)*coefficient[i]);
        //cout<<i+j<<endl;
        //cout<<other.getCoeff(j)<<endl;
        //cout<<coefficient[i]<<endl;
        //cout<<resultCoeff[i+j]<<endl;
    }
}
return Polynomial(newDeg,resultCoeff);
}


string Polynomial::print()
{
string result;
int deg = degree;
for(int i = 0; i<=degree; i++)
{
    if(result == "")
    {
        //cout<<coefficient[i]<<endl;
        result = to_string(coefficient[i]);
        result += "x^";
        result += to_string(deg);
        deg -= 1;
    }
    else //if(coefficient[i] >= 0)
    {
        //cout<<coefficient[i]<<endl;
        result += "+";
        result += to_string(coefficient[i]);
        result += "x^";
        result += to_string(deg);
        deg -= 1;
    }
    //else if(coefficient[i] < 0)
    //{
        //cout<<coefficient[i]<<endl;
    //    result += "-";
    //    result += to_string(coefficient[i]);
    //    result += "x^";
    //    result += to_string(deg);
    //    deg -= 1;
    //}
}
return result;
}

const int Polynomial::getDegree()
{
return degree;
}

const int Polynomial::getCoeff(int index)
{
return coefficient[index];
}

MAIN

#include <iostream>
#include "Polynomial.h"

int main()
{
using namespace std;
int degree = 2;
int coefficients[10] = {2,3,5};
int degree2 = 3;
int coefficients2[10] = {1,5,2,3};

Polynomial test = Polynomial(degree, coefficients);
cout << &test<<endl;//.print()<<endl;
Polynomial test2 = Polynomial(degree2, coefficients2);
cout << &test2<<endl;//.print()<<endl;
Polynomial test3 = test + test2;
cout << &test3<<endl;//.print()<<endl;
Polynomial test4 = test - test2;
cout << &test4<<endl;//.print()<<endl;
Polynomial test5 = test * test2;
cout << &test5<<endl;//.print();
return 0;
}
user2483196
  • 11
  • 2
  • 3
  • 5

2 Answers2

2

Your class does not define a copy constructor. You will need one of these because the default copy constructor does a shallow copy, which only copies the pointer value and not the contents of the coefficient array.

The problem arises when you make copies of your object (which you do all over the place), as both copies will later try to delete the same array when they are destructed.

You need to define an assignment operator also. See the Rule of Three.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • How do I define that? Like I said I am kind of new to C++. – user2483196 Apr 29 '14 at 01:21
  • There is a complete, simple example in the Wikipedia page I linked to. – Greg Hewgill Apr 29 '14 at 01:24
  • I added this: //Copy Constructor Polynomial::Polynomial(const Polynomial &obj) { coefficient = new int; *coefficient = *obj.coefficient; // copy the value } and this to the header. I cannot get it to run now... Polynomial operator*(Polynomial other); Polynomial (const Polynomial& other);//copy constructor Polynomial& operator= (Polynomial& other); //Asignment operator – user2483196 Apr 29 '14 at 02:13
  • That wouldn't be right because `new int;` only allocates *one* integer, but you want an array of size `degree`. Check carefully the example in the Wikipedia page, as it shows what you need to do to successfully make a copy of an array owned by an object. If you still can't get it, refer to a good C++ book or other reference for full explanation. – Greg Hewgill Apr 29 '14 at 02:15
0
    ostream& operator <<(ostream &outs,const Polynomial &poly){
    int i;
    outs << "{";
    for (i= poly.size-1; i >= 0; i--)
        if ((poly.p[i] != 0) && (i==0))
            outs << poly.p[i];
        else if (poly.p[i] > 0)
            outs << poly.p[i] << "x"<< i << " + ";
        else if (poly.p[i] < 0)
            outs << "" << poly.p[i] << "x"<< i << " + " ;

    outs << "}";
    return outs;
}


    Polynomial::Polynomial() {
    int i;
    //cout << "\nconstructor called";
    this->size = 10;
    p = new int[this->size];
    for (i=0; i < this->size; i++)
        p[i] = 0;   
}

    Polynomial::Polynomial(const Polynomial &set) {
    int i;
    this->p = new int[this->size];
    //cout << "\ncopy constructor called";
    for (i=0; i < this->size; i++)
        p[i] = set.p[i];    
}

    Polynomial::~Polynomial() {
    //cout << "\ndestructor called";    
    delete [] p;
}

    Polynomial Polynomial::operator =(const Polynomial &set){
    for (int i=0; i < this->size; i++)
        this->p[i] = set.p[i];  
    return *this;
}

    void Polynomial::display()const{
    for(int i = (this->size)-1; i>=0; i--){
        cout << this->p[i] << "x"<< i << " + ";
    }
}

    void Polynomial::addTerm(int x, int expo) {
    if (expo >= 0 && expo < this->size)
        p[expo] += x;
    / *//   else
    //  if(expo >= this->size)
    //  {
            Polynomial temp;
            temp.size *= 2;
        //  temp = new int[this->size*2];
            for(int i=0; i<temp.size; i++){
                temp.p[i] = this->p[i];
            }
            temp.p[expo] = x;
            this->size*2;
            for(int j=0; j<this->size; j++){
                this->p[j] = temp.p[j];
            }
        }*/
}

    bool Polynomial::operator ==(const Polynomial &poly) const {
    bool ok = true;
    for (int i=0 ; i < this->size; i++) {
        if (this->p[i] != poly.p[i]) {
            ok = false;
            break;
        }
    }
    return ok;
}

    Polynomial Polynomial::operator +(const Polynomial &poly) const {
    Polynomial result;

    int i;
    for (i=0; i < this->size; i++) 
        result.p[i] = this->p[i] + poly.p[i];       
    result.size = this->size;
    return result;
}

    Polynomial Polynomial::operator +=(const Polynomial &poly){

    int i;
    for (i=0; i < this->size; i++) 
        this->p[i] = this->p[i] + poly.p[i];            
    return *this;
}

    Polynomial Polynomial::operator -(const Polynomial &poly) const {
     Polynomial result;
     int i;
     for (i=0; i < this->size; i++) 
        result.p[i] = this->p[i] - poly.p[i];       
     return result;
   }

    Polynomial Polynomial::operator -=(const Polynomial &poly){
    Polynomial result;
    int i;
    for (i=0; i < this->size; i++) 
        this->p[i] = this->p[i] - poly.p[i];            
    return *this;
   }



    //bool Polynomial::operator !=(const Polynomial &poly) const {
    //  return !( *this == poly);}

    Polynomial Polynomial::Derivative( ) const{

    Polynomial result;

    for (int i=1; i < this->size; i++)
        if(p[i] != 0)
            result.p[i-1] = p[i] * i;       
    return result;
   }

    int Polynomial::Evaluate(int x) const{
    int result = 0;

    for(int i=0; i < this->size ; i++){     
        result = result + (p[i] * pow(x,i));        
    }
    return result;
    }
    /*
    Polynomial Polynomial::operator *(const Polynomial &p) const{

    }
Polynomial Polynomial::operator *=(const Polynomial &p){

SO THIS IS MY FUNCTION IMPLEMENTATION....SO ...YEAH..

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
RYE2X
  • 1