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..