Copy of pointers Matrix

1

I'm a little frustrated because I do not understand why this function puts garbage instead of the result. Could someone please tell me what I'm wrong about?

I use the MinGW 5 compiler that comes in Qt. Here I put the headers ...

class matrix
{
    int **array, rows, columns;
public:
    //constructors that allocates memory and fills the array with 0's
    matrix();
    matrix(int side);
    matrix(int width, int height);

    int getRows() { return rows; }
    int getColumns() { return columns; }
    int getMatrix(); //returns the array atrib

    matrix operator+(const matrix &o);
    matrix operator-(const matrix &o);
    matrix operator*(const matrix &o);
    //set matrix methods write input data to the array
    void setMatrix();
    void setMatrix(int side);
    void setMatrix(int width, int height);
    void setRows(int num) { rows = num; }
    void setColumns(int num) { columns = num; }
    void print();

    ~matrix();
};

I'll put the implementation to clarify everything

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

//default constructor without parameters.
matrix::matrix()
{
    //for allocating memory since our array is a pointer.
    rows = columns = 3;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

//default constructor for square matrices.
matrix::matrix(int side)
{
    //for allocating memory since our array is a pointer.
    rows = columns = side;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

//default constructor for defining the rows and columns.
matrix::matrix(int width, int height)
{
    //for allocating memory since our array is a pointer.
    rows = width, columns = height;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

//returns a copy of the array pointer.
int matrix::getMatrix()
{
    int **copy = new int*[rows];

    for (int i = 0; i < rows; i++) {
        copy[i] = new int[columns];
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = 0;
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = array[i][j];
        }
    }
    return **copy;
}

matrix matrix::operator+(const matrix &o)
{
    matrix cpy(rows, columns);
    cpy.array = getArray();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cpy.array[i][j] += o.array[i][j];
        }
    }
    return cpy;
}

matrix matrix::operator-(const matrix &o)
{

}

matrix matrix::operator*(const matrix &o)
{

}

//set matrix as a 3 x 3
void matrix::setMatrix()
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
            std::cin >> array[i][j];
        }
    }
    system("cls");
}

//set matrix as a n x n
void matrix::setMatrix(int side)
{
    rows = columns = side;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
            std::cin >> array[i][j];
        }
    }
    system("cls");
}

//set matrix as a n x m
void matrix::setMatrix(int width, int height)
{
    rows = width, columns = height;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
            std::cin >> array[i][j];
        }
    }
    system("cls");
}

//this will print the matrix without problems.
void matrix::print()
{
    std::cout << std::endl;
    for (int i = 0; i < rows; i++) {
        std::cout << "|";
        for (int j = 0; j < columns; j++) {
            std::cout << "\t" << array[i][j] ;
        }
        std::cout << "\t|";
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

//default destructor for deallocating memory.
matrix::~matrix()
{
    for (int i = 0; i < columns; i++) {
        delete [] array[i];
    }
    delete [] array;

}

The problem is in these two methods.

//returns a copy of the array pointer.
int matrix::getMatrix()
{
    int **copy = new int*[rows];

    for (int i = 0; i < rows; i++) {
        copy[i] = new int[columns];
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = 0;
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = array[i][j];
        }
    }
    return **copy;
}

matrix matrix::operator+(const matrix &o)
{
    matrix cpy(rows, columns);
    cpy.array = getMatrix();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cpy.array[i][j] += o.array[i][j];
        }
    }
    return cpy;
}

and finally the main

#include "matrix.h"

int main()
{
    matrix A(2);
    matrix B(2);
    matrix C(2);
    A.setMatrix();
    B.setMatrix();
    C = A + B;
    C.print();
    return 0;
}

Running Insert 2 times this

Then print me ...

    
asked by Egon Stetmann. 20.07.2017 в 02:54
source

1 answer

2

About your question

EDITED :

The getMatrix function is returning a int when you need to return a int **

On the other hand, when you overload the operator+ , the matrix cpy object that you create is downloaded when you remove it from the stack when you make return cpy; , which invokes the operator delete and your memory is released by the destroyer, hence the cause that you get rare numbers when you go to print the result.

To correct it, I modified the operator+ to return a reference to a dynamically created object.

I'll paste the complete code based on yours. You can also try it in this link , keep in mind that I had to replace the cin to be able to execute the program without typing anything:

#include <iostream>

class matrix
{
    int **array, rows, columns;
public:
    //constructors that allocates memory and fills the array with 0's
    matrix();
    matrix(int side);
    matrix(int width, int height);        

    int getRows() { return rows; }
    int getColumns() { return columns; }
    int **getMatrix(); //returns the array atrib

    matrix& operator+(const matrix &o);
    //set matrix methods write input data to the array
    void setMatrix();
    void print();

    ~matrix();
};

//default constructor without parameters.
matrix::matrix()
{
    //for allocating memory since our array is a pointer.
    rows = columns = 3;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

//default constructor for square matrices.
matrix::matrix(int side)
{
    //for allocating memory since our array is a pointer.
    rows = columns = side;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

matrix::matrix(int width, int height)
{
    //for allocating memory since our array is a pointer.
    rows = width, columns = height;
    array = new int*[rows];

    for (int i = 0; i < rows; i++) {
        array[i] = new int[columns];
    }

    //just for making sure we don't have garbaje.
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = 0;
        }
    }
}

//returns a copy of the array pointer.
int **matrix::getMatrix()
{
    int **copy = new int*[rows];

    for (int i = 0; i < rows; i++) {
        copy[i] = new int[columns];
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = 0;
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            copy[i][j] = array[i][j];
        }
    }
    return copy;
}

matrix& matrix::operator+(const matrix &o)
{
    matrix *cpy = new matrix(rows, columns);
    cpy->array = getMatrix();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            cpy->array[i][j] += o.array[i][j];
        }
    }

    return *cpy;
}

void matrix::setMatrix()
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array[i][j] = (i+1)*(j+1);
        }
    }
}

//this will print the matrix without problems.
void matrix::print()
{
    std::cout << std::endl;
    for (int i = 0; i < rows; i++) {
        std::cout << "|";
        for (int j = 0; j < columns; j++) {
            std::cout << "\t" << array[i][j] ;
        }
        std::cout << "\t|";
        std::cout << std::endl;
    }
    std::cout << std::endl;
}


//default destructor for deallocating memory.
matrix::~matrix()
{
    for (int i = 0; i < columns; i++) {
        delete [] array[i];
    }
    delete [] array;
}

int main()
{
    matrix A(2);
    matrix B(2);
    matrix C(2);
    A.setMatrix();
    B.setMatrix();
    C = A + B;
    C.print();

    return 0;
}

EDITED2 : Assuming that the size of a pointer is the same as a int is very questionable, the size of a int is dependent on the platform and the size of a pointer of the architecture (32bits, 64bits, etc.). It's worth matching you on most occasions ... but that does not mean you're comparing the same things .

    
answered by 20.07.2017 / 18:37
source