I don't understand what's wrong

0

Here is a my own Matrix class:

class Matrix {
private:
    int row;    // rows
    int col;    // columns
    double** matrix;    // pointer to this matrix

public:
    Matrix();
    Matrix(int row, int col);//creating matrix based on two params
    Matrix(double** matx, int row, int col); //Here is a problem
    Matrix(Matrix& N);//copy constructor
    ~Matrix();
    //first, second and fourth constructors works good 

    int getRow();
    int getCol();
    void changeSize(int row, int col);
    void randValues();
    void writeValues();
};

Here is a body of constructor, This constructor need take a exists matrix as parameter and create a new one based on the matrix provided (it's NOT a copy constructor)

Matrix::Matrix(double** matx, int row, int col){
    //allocated memory for new matrix
    matrix = new double*[row];
    for (int i = 0; i < row; i++){
        matrix[i] = new double[col];
    }
    //    //copy values to new matrix
    for(int i=0; i<row; i++)
    {
        for(int k = 0; k < col; k++)
        {
            matrix[i][k] = matx[i][k];
        }
    }
};
int main(){

    double** test = new double *[5];
        for(int i = 0; i < 5; i++){
            test[i] = new double;
        }

        for(int i = 0; i < 5; i++){
            for(int k = 0; k < 5; k++){
                test[i][k] = 0.11;
                cout << test[i][k] << "  ";//just for make me sure if is ok
            }
            cout << "\n";
        }

        Matrix *matx = new Matrix(test,5,5);
        matx->writeValues();

    return 0;
}

And when I run a program they write on console lots of zeros values and few garbage and of the end is: Process returned -1073741819 (0xC0000005) execution time : 2.162 s

c++
matrix
memory
dynamic-memory-allocation
garbage
asked on Stack Overflow Nov 25, 2019 by Dominik • edited Nov 26, 2019 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0