简体   繁体   English

复制构造函数在调用时会抛出std :: bad_alloc

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

I am new c++ programmer. 我是新的C ++程序员。 I want to create a Matrix class in c++ using dynamic programming. 我想使用动态编程在c ++中创建一个Matrix类。 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. 当我调用operator +将其用于矩阵加法时,复制构造函数将引发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]; 当我尝试创建新的动态内存时,在复制构造函数中会出现问题,我可以在其中复制数据:matrix = new int [size]; I don 't understand why. 我不明白为什么。 Here is the whole code: (only the cpp-file) 这是完整的代码:(仅cpp文件)

#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
     }

The first thing I noticed is that in your copy constructor mat_row and mat_col are not initialized. 我注意到的第一件事是在您的副本构造函数中,mat_row和mat_col未初始化。 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. 看来您在operator =()中也有完全相同的问题。

In the code where you indicate that your problem shows up you do not have an lvalue. 在指示问题出现的代码中,您没有左值。 You have mat1+mat2; 你有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. 另外,您的operator +()应该将const ref引用到矩阵,这就是您的实际问题。 Passing by value like you currently are will cause a temporary copy which is created through the copy constructor. 像您当前那样传递值将导致通过副本构造函数创建的临时副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM