0

A destructor to free the dynamic memory allocated for a matrix

~ UTM ( ) { … }

properly frees any dynamically allocated memory for the sparse matrix object.

Creation of matrix has already been achieved, I am stuck on how to delete the memory allocated to the new matrix in a separate class through the destructor of another class.

struct node
{
    int data;
    node *next;
};


class UTM
{
public:
    int row, col;
    string name;
    node *head, *tail;

    UTM(string name)
    {
        this->name = name;

        head = NULL;
        tail = NULL;
    }

    UTM(int row, int col, string name)
    {
        this->row = row;
        this->col = col;
        this->name = name;

        head = NULL;
        tail = NULL;
    }

    ~UTM()
    {
        delete sumMatrix;
    }


    UTM *UTM::sumUTM(UTM &other)
    {
      UTM *sumMatrix = new UTM(row, col, "Addition Matrix Result");

      node *temp1 = other.head;
      node *temp2 = this->head;

      while(temp2 != NULL)
      {
        int sum = temp1->data + temp2->data;
        sumMatrix->inputNode(sum);

        temp1 = temp1->next;
        temp2 = temp2->next;
      }

      return sumMatrix;
    }

How do I free the allocated memory through the destructor, I am stuck on only this part..

bruno
  • 32,421
  • 7
  • 25
  • 37
Anish Ardjoon
  • 91
  • 1
  • 8
  • 2
    `sumMatrix` is not available to the destructor. Do you mean for `sumMatrix` to be a member variable? Or is `sumMatrix` the instance? I think you need to stop and discuss your intentions with your [Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – user4581301 Jan 22 '19 at 22:00
  • 2
    I think I'm unraveling your problem. You've wandered into an area of great pain. To dig your way out, 1) The destructor must delete the contained `node`s, not another instance of the class. 2) Make UTM [Rule of Three /Five Compliant](https://en.cppreference.com/w/cpp/language/rule_of_three) This means a Copy constructor and an assignment operator at a minimum. 3) Return UTM by value: `UTM UTM::sumUTM(UTM &other)` and `UTM *sumMatrix = new UTM(row, col, "Addition Matrix Result");` becomes `UTM sumMatrix(row, col, "Addition Matrix Result");` – user4581301 Jan 22 '19 at 22:09
  • 1
    The move constructor and move assignment operator of the Rule of Five can help greatly with performance but [Copy Elision](https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) will probably be good enough . – user4581301 Jan 22 '19 at 22:13
  • 1
    destructor frees resources allocated in constructor. You have allocation in a member function, so either the function or its user is responsible to free it. destructor does not help here. – balki Jan 22 '19 at 22:32
  • @balki destructor is responsible for releasing any resources [owned](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) by the object, no matter where they were allocated. Wait. My apologies. You are referring to `sumMatrix` which has no owner (and probably shouldn't be a dynamic allocation in the first place). – user4581301 Jan 22 '19 at 22:42

0 Answers0