amitakCs
amitakCs

Reputation: 467

Copy constructor throws a std::bad_alloc, when it is called

I am new c++ programmer. I want to create a Matrix class in c++ using dynamic programming. I had a problem with copy constructor. When I call operator + to use it for matrix-addition, the copy constructor throws a bad_alloc-Exception. The problem occurs in the copy-constructor, when i try to create a new dynamic memory, where i can copy my data: matrix= new int [size]; I don 't understand why. Here is the whole code: (only the cpp-file)

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

Matrix::Matrix(int m, int n){
    mat_row=m;
    mat_col=n;
    matrix = new int[mat_row*mat_col];
    //initialization
    for(int i=0;i<mat_row*mat_col;i++){
       matrix[i]=0;
     }
 }

 //copy-Constructor
  Matrix::Matrix(const Matrix& mat){
      int size=mat_row*mat_col;
      matrix= new int [size];
      for(int i=0;i<size;i++){
          matrix[i]=mat.matrix[i];
      }
    }

  Matrix::~Matrix(){
      if(matrix){
          delete [] matrix;
      }
  }


  //assigment operator
  Matrix& Matrix::operator=(const Matrix& mat){
      std::cout<<"assignment-operator is used"<<std::endl;
      if(this!=&mat){
          int size=mat_row*mat_col;
          for(int i=0;i<size;i++){
             matrix[i]=mat.matrix[i];
          }
      }
      return *this; 
  }

  void Matrix::set(int m, int n, double value){
      if(matrix){
          matrix[m*mat_col+n]=value;
      }
  }

  double Matrix::get(int m, int n){
      return matrix[m*mat_col+n];
  }


 Matrix Matrix::operator+(Matrix mat){
 /*
    Matrix resultMatrix(mat_row, mat_col);
    for(int i=0;i<mat_row;i++){
        for( int j=0;j<mat_col;j++){
            resultMatrix->set(i,j,this->get(i,j)+mat.get(i,j));
         }
    }
    return *this;
  */
  }



    int main(){
        Matrix mat1(3,3);
        mat1.set(0,0,11);
        mat1.set(1,1,22);
        mat1.set(2,2,33);
        mat1.print();
        std::cout<<std::endl;   

       Matrix mat2(3,3);
       mat2=mat1;
       mat2.print();
       std::cout<<std::endl;
       mat2.set(0,1,55);
       mat2.set(1,1,110);
       mat2.set(2,2,220);
       mat2.print();

       mat1+mat2; //the problem occurs when this row will be executed
     }

Upvotes: 1

Views: 1492

Answers (1)

hvanbrug
hvanbrug

Reputation: 1301

The first thing I noticed is that in your copy constructor mat_row and mat_col are not initialized. They should be getting set from the values that are in the object you are copying before you multiply them together to make size. If you don't do that first then the existing values are undefined and likely very large values, causing the new to fail.

//copy-Constructor
  Matrix::Matrix(const Matrix& mat){
      mat_row = mat.mat_row;
      mat_col = mat.mat_col;
      int size=mat_row*mat_col;
      matrix= new int [size];
      for(int i=0;i<size;i++){
          matrix[i]=mat.matrix[i];
      }
    }

It looks like you have the exact same problem in you operator=() as well.

In the code where you indicate that your problem shows up you do not have an lvalue. You have mat1+mat2; so the result of that addition ends up being unused.

Also, your operator+() should be taking a const ref to the matrix and that is your actual issue. Passing by value like you currently are will cause a temporary copy which is created through the copy constructor.

Upvotes: 1

Related Questions