处理矩阵(数组)的分段错误错误

Segmentation Fault Error Dealing with Matrices (Arrays)

本文关键字:错误 分段 数组 处理      更新时间:2023-10-16

我正在尝试引用我构建的 2D 数组对象,但由于某种原因,我遇到了分割错误,并且无法找出原因。到目前为止,我已经推断出错误在于我的operator<<函数,但我无法弄清楚要更改什么。

主.cpp:

int main() {    
My_matrix m1(3, 2);
cout << "Before" << endl;
m1(0,0) = 1; //causes segfault
cout << "After" << endl;
m1(0,1) = 2; //causes segfault
m1(1,0) = 3; //causes segfault
m1(1,1) = 4; //causes segfault
m1(2,0) = 5; //causes segfault
m1(2,1) = 6; //causes segfault
cout << "Checkpoint 1" << endl;
My_matrix m2(); //works
cout << "Checkpoint 2" << endl;
cout << m1.number_of_rows() << endl; //works
cout << "Checkpoint 3" << endl;
cout << m1; //Segfault
cout << "Checkpoint 4" << endl;
cout << m1; //Segfault
cout << "Checkpoint 5" << endl;
cout << m2; //Segfault
cout << "Checkpoint 6" << endl;
}

My_matrix.cpp:

#include "My_matrix.h"
#include <stdexcept>
My_matrix::My_matrix()
{
  n = 0;
  m = 0;
  ptr = nullptr;
}
My_matrix::My_matrix(int n1, int m1)
{
  n = n1;
  m = m1;
  ptr = new int*[n];
  for(int i = 0; i < n; i++) {
      ptr[i] = new int[m];
  }
  for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
          ptr[i][j] = 0;
      }
  }
}
My_matrix::My_matrix(const My_matrix& mat)
{
  this->n = mat.n;
  this->m = mat.m;
  ptr = new int*[n];
  for(int i = 0; i < n; i++) {
      ptr[i] = new int[m];
  }
  //----- copy elements over to new matrix -----//
  for (int i = 0; i < mat.n; i++) {
      for (int j = 0; j < mat.m; j++) {
          this->ptr[n][m] = mat.ptr[n][m];
      }
  }
}
My_matrix::~My_matrix()
{
  clear();
}
My_matrix& My_matrix::operator=(const My_matrix& mat)
{
  clear();
  this->n = mat.n;
  this->m = mat.m;
  ptr = new int*[n];
  for(int i = 0; i < n; i++) {
      ptr[i] = new int[m];
  }
  //----- copy elements over -----//
  for (int i = 0; i < mat.n; i++) {
      for (int j = 0; j < mat.m; j++) {
          this->ptr[n][m] = mat.ptr[n][m];
      }
  }
}
int My_matrix::number_of_rows() const
{
  return n;
}
int My_matrix::number_of_columns() const
{
  return m;
}
int* My_matrix::operator()(int i) const
{
  return ptr[i];
}
int& My_matrix::operator()(int i, int j) const
{
  return ptr[n][m];
}
int& My_matrix::operator()(int i, int j)
{
  return ptr[n][m];
}
int& My_matrix::elem(int i, int j) const
{
  if (i < 0 || i >= n) throw out_of_range("Out of range");
  if (j < 0 || j >= m) throw out_of_range("Out of range");
  return ptr[n][m];
}
int& My_matrix::elem(int i, int j)
{
  if (i < 0 || i >= n) throw out_of_range("Out of range");
  if (j < 0 || j >= m) throw out_of_range("Out of range");
  return ptr[n][m];
}
void My_matrix::clear()
{
    for (int i = 0; i < n; i++) {
        delete[] ptr[i];
    }
    delete[] ptr;
    n = 0;
    m = 0;
}
ostream& operator<<(ostream& out, const My_matrix& mat)
{
  for (int i = 0; i < mat.number_of_rows(); i++) {
      for (int j = 0; j < mat.number_of_columns(); j++) {
          out << mat(i, j) << " "; //This line is the culprit ****
      }
      out << endl;
  }
  return out;
}

正如 @WhozCraig 和 @O'Neil 在注释中暗示的那样,我的代码中有一些拼写错误,这些拼写错误引用了不在我创建的矩阵范围内的索引。我没有使用迭代器/函数参数,而是使用了 nm ,它们表示矩阵的大小,这显然比最大索引大一个。示例包括:

int& My_matrix::elem(int i, int j) const
{
  if (i < 0 || i >= n) throw out_of_range("Out of range");
  if (j < 0 || j >= m) throw out_of_range("Out of range");
  return ptr[n][m]; //<--- should be return ptr[i][j];
}

以及

My_matrix::My_matrix(const My_matrix& mat)
{
  this->n = mat.n;
  this->m = mat.m;
  ptr = new int*[n];
  for(int i = 0; i < n; i++) {
      ptr[i] = new int[m];
  }
  //----- copy elements over to new matrix -----//
  for (int i = 0; i < mat.n; i++) {
      for (int j = 0; j < mat.m; j++) {
          this->ptr[n][m] = mat.ptr[n][m]; //<--- should be this->ptr[i][j] = mat.ptr[i][j];
      }
  }
}

通过纠正这些差异,我的问题得到了解决。