Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling destructor

Tags:

c++

Hi I was learning operator overloading.. i noticed the program crashes when calling the destructor.. any help is appreciated. Thanks

#include<iostream>
using namespace std;

class Overload
{
private:
    int a, *b;
public:
    Overload():a(0) {cout << "default\n"; };
    Overload(int x);
    Overload operator+(Overload & rhs);
    int geta();
    int* getb();
    void setb();
    void PrintVals(const Overload & val);
    ~Overload();
};


Overload::Overload( int x)
{
    cout << "Parameterized constructor\n";
    a = x;
    b = new int[a];
}


int Overload::geta()
{
    return a;
}


int* Overload::getb()
{
    return b;
}

void Overload::setb()
{
    int val;
    cout << "setting b values\n";
    for (int i = 0 ; i <= a; i++)
    {
        cin >> val;

        b[i] = val;
    }
}



Overload Overload::operator+(Overload & rhs)
{
    Overload temp;
    temp.a  = this->a + rhs.a;
    temp.b = new int[temp.a];
    *temp.b =  *(this->b) + *(rhs.b);

    cout << "inside overload + vale of LHS:" << *(this->b) << endl;
    cout << "inside overload + vale of RHS:" << *(rhs.b) << endl;
    cout << "inside overload + vale of temp:" << *temp.b << endl;
    cout << "Address of b(temp):" << temp.b << endl;


    temp.b++;
    this->b++;
    rhs.b++;

    *temp.b =  *(this->b) + *(rhs.b);

    cout << "inside overload + vale of LHS:" << *(this->b) << endl;
    cout << "inside overload + vale of RHS:" << *(rhs.b) << endl;
    cout << "inside overload + vale of temp:" << *temp.b << endl;

    cout << "Address of b(temp):" << temp.b << endl;



    return temp;
}


 Overload::~Overload()
 {
 cout << "Destructor \n";
 cout << "Address deallocated b:" << b;
 delete [] b;
 }



void Overload::PrintVals(const Overload & val)
{
    int val1, *val2;

    val1 = this->a;
    val2 = this->b;

    cout << "Printing values: a: " << val1 << " b:" << *val2;
}


int main()
{
    Overload X(1),Y(1),Z;
    int val1, *val, val2, val3;
    //Z = X + Y;

    val1 = Y.geta();
    val2 = X.geta();

    Y.setb();


    val = Y.getb();
    printf("val of y  b: %d\n",*val);
    printf("val of x  a: %d\n",val1);

    X.setb();

    val = X.getb();
    printf("val of x b: %d\n",*val);
    printf("val of x a: %d\n",val2);


    Z = X + Y;

    val  = Z.getb();
    val3 = Z.geta();
    val--;

    for( int i = 0; i < val3; i++)
    {
        printf("address of (b) Z: %p \n",val);
        printf("val of z b: %d\n",*val);
        printf("val of z a: %d\n",val3);
        val++;

    }

}

OUTPUT:... Destructor Part1(4815,0x7fff73def300) malloc: * error for object 0x100200004: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Address deallocated b:0x100200004(lldb)

like image 276
jramacha Avatar asked Sep 10 '26 07:09

jramacha


1 Answers

You should implement copy constructor and assignment operator also.

like image 101
Andrew Svetlov Avatar answered Sep 11 '26 20:09

Andrew Svetlov