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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dominik
  • 9
  • 2
  • Where do you store `row` and `col` in your constructor `Matrix::Matrix`? There are the arguments `row` and `col` and the members `row` and `col` (implicitly `this->row` and `this->col`) but I cannot see any initialization or assignment. – Scheff's Cat Nov 25 '19 at 10:55
  • _Rest of constructors works good_ That was over-optimistic. ;-) – Scheff's Cat Nov 25 '19 at 10:59
  • 1
    @Amadeus No. `double** test = new double *[5];` is correct. The OP wants a array of pointers (to arrays). It's rather the inner `test[i] = new double;` what makes me afraid... It should be `test[i] = new double[5];` – Scheff's Cat Nov 25 '19 at 11:00
  • @Amadeus Actually, `**` shouldn't be used in C++. A (real) matrix can be stored with a 1d array (e.g. `std::vector`) where 2d access is covered by getter/setter resp. – Scheff's Cat Nov 25 '19 at 11:04
  • 1
    Of course, this is not a copy constructor. It doesn't even has the signature of one. However, copy-constructor or not, where is the initialization of members `row` and `col`? (Answer: It's missing!) – Scheff's Cat Nov 25 '19 at 11:05
  • 1
    [SO: A proper way to create a matrix in c++](https://stackoverflow.com/a/618547/7478597) – Scheff's Cat Nov 25 '19 at 11:08
  • @Scheff, I added two lines to the constructor Matrix::Matrix this->row = row; and this->col = col and now it does works :) – Dominik Nov 25 '19 at 11:16
  • 1
    Ah, you got the message. ;-) Please, have also an eye on what I wrote about `test[i] = new double;`. As it is now, it's U.B. If it works - this is just an accident. (U.B. doesn't exclude this case but that doesn't change the fact that it's U.B.) U.B. -> [Undefined Behavior](https://stackoverflow.com/a/4105123/1505939) – Scheff's Cat Nov 25 '19 at 11:18
  • Yes, i changed it too, to test[i] = new double[5]; – Dominik Nov 25 '19 at 11:22
  • 2
    If I were making this Matrix class, I would not use `new`. I'd use a `std::vector` member variable, and use that for a flat representation of the matrix internally. Instead of `double** matx` I'd use `std::initializer_list`. All the memory management details go away. Copy contructor, destructor can be `= default;`. The row and col should be typed as `size_t`. – Eljay Nov 25 '19 at 12:54

0 Answers0