m0_74300955 2023-02-11 16:16 采纳率: 75%
浏览 20
已结题

C++矩阵类定义,遇到了Segmentation fault

OJ系统一直提示:Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存区域

但是一直检查不出来哪里有错误。二维动态数组这个问题困扰我好久了,求指点!

#include<iostream>
using namespace std;
//矩阵类定义
class Matrix
{
public:
    Matrix(int, int);
    void set(int, int);
    friend istream& operator>>(istream &, Matrix&);
    friend ostream& operator<<(ostream&, const Matrix&);
    Matrix& operator*(Matrix&);
    void DiagonalOrder();
    void setZeros();
    
protected:
    int** mat;                                     //矩阵(二位数组)
    int row;                                        //行数
    int col;                                         //列数
};
Matrix temp(0,0);                               //定义全局对象,用于接受矩阵乘法结果
void Matrix::set(int n, int m)                
{
    row = n;
    col = m;
    mat = new int* [n];
    for (int i = 0; i < n; i++)
    {
        mat[i] = new int[m];
    }
    if (mat == NULL)
    {
        cout << "allocation failure!\n";
        return;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            mat[i][j] = 0;
        }
    }
}
Matrix::Matrix(int n, int m)                       //构造函数,初始化行数列数与矩阵
{
    row = n;
    col = m;
    if (m > 0 && n > 0)
    {
        mat = new int* [n];
        for (int i = 0; i < n; i++)
        {
            mat[i] = new int[m];
        }
        if (mat == NULL)
        {
            cout << "allocation failure!\n";
            return;
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                mat[i][j] = 0;
            }
        }
    }
    else
        mat = NULL;
}
istream& operator>>(istream& input, Matrix& a)        //重载输入运算符
{
    for (int i = 0; i < a.row; i++)
    {
        for (int j = 0; j < a.col; j++)
        {
            input >> a.mat[i][j];
        }
    }
    return input;
}
ostream& operator<<(ostream& output,const Matrix& a)     //重载输出运算符
{
   
    for (int i = 0; i < a.row; i++)
    {
        for (int j = 0; j < a.col-1; j++)
        {
            output << a.mat[i][j] << " ";
        }
        cout << a.mat[i][a.col - 1] << endl;
    }
    return output;
}

Matrix& Matrix::operator*(Matrix&a)                        //重载矩阵乘法
{
    if (col == a.row)
    {
        temp.set(row, a.col);
        
        for (int i = 0; i < temp.row; i++)
        {
            for (int j = 0; j < temp.col; j++)
            {
                int sum = 0;
                int k = 0,o = 0;
                while (k < col && o < a.row)
                {
                    sum += mat[i][k] * a.mat[o][j];
                    k++; o++;
                }
                
                temp.mat[i][j] = sum;
            }
        }
        return temp;
    }
    else
    {
        cout << "can't multiply!" << endl;
        
    }
}
void Matrix::DiagonalOrder()                                               //之字形打印
{
    
        cout << mat[0][0] << " ";
    
       
        int is_up = 1;
        int x = 0, y = 0;
        while (x >= 0 && x <= row - 1 && y >= 0 && y <= col - 1)
        {
            if (x == 0)
            {
                if (y == 0)
                {
                    y++;
                    is_up = 0;                  
                }
                else if(y<col-1)
                {
                    if (is_up == 0)
                    {
                        x++; y--;
                        
                    }
                    else
                    {
                       y++;
                       is_up = 0;
                    }
                }
                else
                {
                    is_up = 0;
                    x++;
                }
            }
            else if (x == row - 1)
            {
                if (y>=0&&y<col-1)
                {
                    if (is_up)
                    {
                        x--; y++;
                    }
                    else
                    {
                        y++;
                        is_up = 1;
                    }
                }
                
            }

            else if (y == 0)
            {
                if (is_up == 1)
                {
                    x--; y++;
                }
                else
                {
                    x++;
                    is_up = 1;
                }
            }
            else if (y == col - 1)
            {
                if (is_up == 1)
                {
                    x++;
                    is_up = 0;
                }
                else
                {
                    x++; y--;
                }
            }
            else
            {
                if (is_up ==1)
                {
                    x--; y++;
                }
                else
                {
                    x++; y--;
                }
            }

            if (x == row - 1 && y == col - 1)
            {
                cout << mat[x][y] << endl;
                break;
            }
            else
                cout << mat[x][y] << ' ';
        }
       
       
    
   

}
void Matrix::setZeros()                                            //将矩阵中有0的行和列全部置0
{
    int** temp = new int* [row];
    for (int i = 0; i < row; i++)
    {
        temp[i] = new int[col];
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            temp[i][j] = mat[i][j];
        }
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (temp[i][j] == 0)
            {
                for (int k = 0; k < col; k++)
                    mat[i][k] = 0;
                for (int o = 0; o < row; o++)
                    mat[o][j] = 0;
            }
        }
    }
    delete[]temp;

}

int main() {

    int n, m;

    while (cin >> m >> n) {

        Matrix mat1(m, n);

        cin >> mat1;



        int x, y;

        cin >> x >> y;

        Matrix mat2(x, y);

        cin >> mat2;



        Matrix res = mat1 * mat2;

        cout << res;



        mat1.DiagonalOrder();



        mat1.setZeros();

        cout << mat1;

    }

    return 0;

}

  • 写回答

2条回答 默认 最新

  • 东京小M 2023-02-11 16:56
    关注

    这个问题很有可能是数组越界或者指针引用了无效的内存,你可以尝试在程序中添加一些调试代码,来追踪程序的执行情况。

    具体的,你可以使用以下方法来定位错误:

    在代码中加入许多 cout 语句,来追踪变量的值,以及程序的执行情况,这有助于你了解程序是如何执行的。

    使用断点调试,来检查程序在执行时是否出现错误。

    使用 assert 语句,如果一个表达式的结果为 false,则程序将中止执行,这有助于你定位错误。

    希望这些建议能帮助你找到问题所在。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 创建了问题 2月11日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题