-1

I'm learning cpp and I've tried to always allocate memory correctly but now after adding deconstructor, it crashes with HEAP CORRUPTION DETECTED. I want to be able to have W1 smaller than W2. Right now, program works (without destructor ofc) if W1 is bigger than W2, but otherwise it cuts result up to the W1 size. I use Visual Studio 2019. Here is my code:

#include <iostream>

using namespace std;
class Polynomial
{
private:
    int degree_; 
    int* ptr= nullptr;
public:
    Polynomial()
    {
        int* ptr = new int[1];
        ptr[0] = 0;
    }
    Polynomial(int degree)
    {
        degree_ = degree;
        ptr = new int[degree_ + 1]; //+1 for the coefficient next to x^0

    }
    Polynomial(const Polynomial& oldobj)
    {
        degree_ = oldobj.degree_;
        ptr = new int[degree_ + 1];
        *this = oldobj;
        //for (int i = 0; i < degree + 1; i++)
            //ptr[i] = oldobj.ptr[i];

    }
    ~Polynomial()
    {
        delete[]ptr;
    }
    Polynomial& operator+(const Polynomial& tempobj)
    {
        for (int i = 0; i <= tempobj.degree_; i++)
        {
            ptr[i] += tempobj.ptr[i];
        }

        return *this;
    }
    Polynomial& operator=(const Polynomial& tempobj)
    {
        degree_ = tempobj.degree_;
        ptr = tempobj.ptr;

        return *this;
    }
    friend istream& operator>>(istream& in, Polynomial& obj);
    friend ostream& operator<<(ostream& oou, const Polynomial& obj);
};

istream& operator>>(istream& in, Polynomial& obj)
{
    for (int i = obj.degree_; i >= 0; i--)
    {
        if (i == 0)
        {
            cout << "Last Coefficient: ";
            cin >> obj.ptr[i];
            break;
        }
        cout << "Coefficient in x^" << i << ": ";
        cin >> obj.ptr[i];

    }
    return in;
}
ostream& operator<<(ostream& out, const Polynomial& obj)
{
    cout << "f(x)= ";
    for (int i = obj.degree_; i >= 0; i--)
    {
        cout << obj.ptr[i] << "x^" << i << " ";
    }

    return out;
}
int main()
{
    Polynomial W1(2), W2(4), W3; //I want W1 to be able to swap places with W2.
                              //Right now it works only when W2 is smaller than W1;
    cin >> W1;
    cout << endl << W1<<endl;
    cin >> W2;
    cout << endl << W2 << endl;
    W3 = W1 + W2;
    cout << endl << W3;
    return 0;
}

So first, how can I fix the destructor? Thanks for your time.

RektLeft
  • 93
  • 1
  • 8
  • 1
    Check your `operator+` logic. What happens if `tempobj`'s degree is larger than `this` object's degree? – TheUndeadFish Apr 22 '21 at 04:57
  • Worse, what values is the member `degree_` taking on when the *default* constructor is actually utilized? And your assignment operator is a recipe for (a) leaking memory and (b) a double-delete on a now-shared pointer. – WhozCraig Apr 22 '21 at 05:02
  • You also have a problem in your operator=, as you assign the same pointers to two different polynoms. When the first destructor is called everything will be fine, but when the second destructor is called, the polynom will try to deallocate a portion of memory which was alredy deallocated, hence your heap corruption. – Laurent Jospin Apr 22 '21 at 05:06
  • @TheUndeadFish Oh this is the edited code where I cut out the if/else part in operator+ to see if it helps. This is what it was like: if(degree_<=tempobj.degree_) else if (degree_ > tempobj.degree_) – RektLeft Apr 22 '21 at 05:38
  • 1
    You'd probably be better off just using a vector. – Kyle Apr 22 '21 at 05:48
  • @LaurentJospin so what should the = operator look like in my case? – RektLeft Apr 22 '21 at 05:50
  • @RektLeft The challenge with C++ is that sometimes to consequences of a mistake can show up far separated from the mistake itself. Heap corruption can easily be the result of writing past the end of a dynamically allocated array. The code you showed is vulnerable to that. If you have other code that's more correct, then show that instead. – TheUndeadFish Apr 22 '21 at 17:01

1 Answers1

0

Your destructor is fine. The problem is your plus operator. You need to create an array capable of storing all the coefficients and return a copy of the object. Currently you mutate your first operand. There are other issues, but they are out of this question.

SIREN
  • 161
  • 1
  • 5