0

Having issues with this program. I've been trying to identify all the areas where data is created, then tried to destroy it likewise after using it. I've put a lot of delete[] coef (coef is a pointer double array to hold coefficients). The program will run half the time, then crash half of the time. The algorithms are correct for what I need to do; however, I just can't wrap my head around the crash issue, nor identify the memory issue. Any help is greatly appreciated!

```
//multiply function used within the overloading, the multiplication takes one polynomial as constant, then breaks apart the second part one term at a time and uses a private constructor to shift the first polynomial by each term's degree and scale it by the coefficient of that term.
void Polynomial::mult(const Polynomial& pol)
{
Polynomial r;
r.degree=degree+pol.degree;
delete[] r.coef;
r.coef=new double[r.degree+1];
for(int i=r.degree;i>=0;i--)
r.coef[i]=0.0;
Polynomial copy;
delete[] copy.coef;
copy=*this;
for(int i=pol.degree;i>=0;i--)
{
Polynomial p1(copy,pol.coef[i],i);
r.add(p1);
p1.~Polynomial();
}
delete[] coef;
*this=r;
r.~Polynomial();
copy.~Polynomial();
}
//overloaded operator to multiply two polynomial objects, while keeping the original objects the same (const).
//called as a friend function
Polynomial operator*(const Polynomial& p1, const Polynomial& p2)
{
Polynomial pol1(p1);
Polynomial pol2(p2);
pol1.mult(pol2);
return pol1;
pol1.~Polynomial();
pol2.~Polynomial();
}
//how it's called in main
cout<<"("<<p1<<")*("<<p2<<")="<<p1*p2<<endl;
//how it's outputted
ostream& operator<<(ostream& stream, const Polynomial& pol)
{
//outputting using "stream<<output"
return stream;
}
```

*Edited 6 Years Ago by gnarlyskim*: n/a