Don Shanil
Don Shanil

Reputation: 161

Destructor not called and issue with program exiting abnormally

I have the following code for defining a class Matrix. The definitions in the header file are as follows:

#ifndef MATRIX_H
#define MATRIX_H
/* General matrix class */

class Matrix
{
    public:
        Matrix(int m, int n, double ini);
        virtual ~Matrix();
        Matrix(const Matrix& other); //copy ctor
        Matrix& operator=(const Matrix& other); //assignment operator;
        double operator()(int i, int j) const; //access element in the matrix
        double& operator() (int i, int j); //set element in the matrix
        friend Matrix operator+(const Matrix& mat1, const Matrix& mat2); //Matrix addition
        friend Matrix operator+(const Matrix& mat1, double a); //scaler multiplication
        int dimension() const {return rows*cols;} //getter method for dimension
    protected:
    private:
        int rows; //number of rows in the matrix
        int cols; //number of cols in the matrix
        double* d; //pointer to the representation of the matrix
};

The implementation of the parts relevant to the question are shown below.

#include "Matrix.h"
#include<iostream>

Matrix::Matrix(int m, int n, double ini):rows{m},cols{n},d{new double[m*n]}
{
        //ctor
        double* p = d;
        for(int i=0;i<rows*cols;i++)
        {
            *p++ = ini;
        }
}

Matrix::~Matrix()
{
    //dtor
    delete []d;
}


Matrix& Matrix::operator=(const Matrix& rhs)
{
    if (this == &rhs) return *this; // handle self assignment
    if (rows*cols<=rhs.rows*rhs.cols)
    {
        delete []d;
        d = new double[rhs.rows*rhs.cols];
        rows = rhs.rows;
        cols = rhs.cols;
        for(int i=0;i<rows*cols;i++)
        {
            d[i] = rhs.d[i];
        }
    }
    //assignment operator
    return *this;
}

double Matrix::operator()(int i, int j) const
{
    return d[rows*i + j];
}

double& Matrix::operator()(int i, int j)
{
    return d[rows*i+j];
}

Now, I have a simple test application that creates a matrix, assigns values to the elements in the matrix, as well reads the value of an element (given a row and a column number).

    #include <iostream>
    #include "Matrix.h"
    using namespace std;

    int main()
    {
        int i,j;
        Matrix A(3,2,0.0);
        cout<<A.dimension()<<endl;
    // assign values to matrix elements
        for (i=0;i<3;i++)
            {
                for (j=0;j<2;j++) A(i,j) = 0.1*i*j;
            }

            // access matrix elements
        double sum = 0.0;
        for (i=0;i<3;i++) {
            for (j=0;j<2;j++) sum += A(i,j); }
        cout << "The sum of the matrix elements is ";
        cout << sum << endl;
        return 0;
    }

My issue is that while everything compiles without problems, when run, the main function freezes -- the "sum" above is computed though. Was wondering if this is due to the destructor not being called or being unable to be called. Much appreciated if anyone has any ideas.

Upvotes: 0

Views: 60

Answers (1)

Jerome
Jerome

Reputation: 527

I think your code are wrong in operator()

double Matrix::operator()(int i, int j) const
{
    return d[cols*i + j];
}

double& Matrix::operator()(int i, int j)
{
    return d[cols*i+j];
}

And you overflow the array d[]

Upvotes: 1

Related Questions