处理矩阵(数组)的分段错误错误
Segmentation Fault Error Dealing with Matrices (Arrays)
我正在尝试引用我构建的 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 在注释中暗示的那样,我的代码中有一些拼写错误,这些拼写错误引用了不在我创建的矩阵范围内的索引。我没有使用迭代器/函数参数,而是使用了 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]; //<--- 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];
}
}
}
通过纠正这些差异,我的问题得到了解决。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)