Question: // ************************************************************************** // CPSC2620 Spring 2020 - Prof Zhang I can't seem to use my destructor as when I include my deallocate function, it is

// ************************************************************************** // CPSC2620 Spring 2020 - Prof Zhang

I can't seem to use my destructor as when I include my deallocate function, it is called multiple times and i lose memory. When i comment out the deallocate function in the destructor the program will run perfectly but with memory Leaks.

Any information would help.

#include"./matrixExt.h" #include #include #include

using namespace std;

//**************************************************************************** // Default constructor. Default dimensions are 2 x 2 //**************************************************************************** CMatrixExt::CMatrixExt(int d) { assert(d>=0); n = d; M = new int*[d]; // blck is an array of pointers to ints for(int i=0; i

//**************************************************************************** // copy constructor //**************************************************************************** CMatrixExt::CMatrixExt(const CMatrixExt &matrix){

M = allocateMemory(matrix.n); for(int i=0; i

cout<

//**************************************************************************** // Function to read from an input stream //**************************************************************************** void CMatrixExt::readMatrix() { for(int i = 0; i> *(*(M+i)+j); // same as M[i][j] } } //**************************************************************************** // Function to print a matrix //**************************************************************************** void CMatrixExt::printMatrix() { for(int i = 0; i

//**************************************************************************** // Functions to "return" return the dimensions of the Matrix //**************************************************************************** int CMatrixExt::getDimension() const { return n; } //**************************************************************************** // returns an element from the matrix //**************************************************************************** int CMatrixExt::getElement(int i,int j){ return M[i][j]; }

//**************************************************************************** // Functions to replace an element and returns the old value //**************************************************************************** int CMatrixExt::replaceElementAt(int i, int j, int newInt) { assert(i>0 && i <= n && j>0 && j<=n); int oldVal = *(*(M+i-1)+j-1); *(*(M+i-1)+j-1) = newInt; return oldVal; }

//**************************************************************************** // Functions to resize a matrix. //**************************************************************************** void CMatrixExt::resizeMatrix(int newSize) { assert(newSize >=1); int oldSize = n; if(!(newSize==n)) // do nothing if newSize is same as current dimension { int** temp = allocateMemory(newSize); n = newSize; if(oldSize < newSize) { for(int i=0; i

//**************************************************************************** // makes an identity matrix corresponding to n (dimension) //**************************************************************************** void CMatrixExt::makeIdentity() { for(int i=0;i

//**************************************************************************** //will copy a matrix element by element //**************************************************************************** void CMatrixExt::copyMatrix(const CMatrixExt &matrix) { assert(n=matrix.n); M = matrix.M; }

//**************************************************************************** // Add two matricies of the same size //**************************************************************************** CMatrixExt CMatrixExt::addMatrix(const CMatrixExt &matrix)const { assert(n==matrix.n); CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** // adds a constant value to every element //**************************************************************************** CMatrixExt CMatrixExt::addMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** //Subtracts the elements of two matricies the same size //**************************************************************************** CMatrixExt CMatrixExt::subtractMatrix(const CMatrixExt &matrix)const { assert(n==matrix.n); CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** //Subtracts the same value from every element in matrix //**************************************************************************** CMatrixExt CMatrixExt::subtractMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

//**************************************************************************** //multiply two matrices that are compatible //**************************************************************************** CMatrixExt CMatrixExt::multiplyMatrix(const CMatrixExt &matrix) { CMatrixExt mul(3); for(int i =0;i

//**************************************************************************** //multiply matrix elements by the same value //**************************************************************************** CMatrixExt CMatrixExt::multiplyMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

//************************************************************************** // Private helper function to allocate new memory //**************************************************************************** int** CMatrixExt::allocateMemory(int d) { int** temp; temp = new int*[d]; for(int i=0; i

//**************************************************************************** // Private helper function to deallocate a memory block from the heap //****************************************************************************

void CMatrixExt::deallocateMemory(int **matrix, int d){ for(int i = 0; i < d; i++) { delete [] matrix[i]; } delete [] matrix;

matrix = nullptr; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!