简体   繁体   中英

Segmentation Fault in Linux C++ but code is working in windows

AoA, Here is the code of multiplication of two matrices, which runs fine under 3x3 matrices but gives error on exceding row or column of 3x3, like on 3x4 and 4x3 it gives the error "segmentation fault"

#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

struct matrix
{
    int** mat;
    int row;
    int col;
    matrix(int m,int n)
    {
        row = m;
        col = n;
        mat = new int*[row];
        for( int i=0;i<row;i++ )
        {
            mat[i] = new int[col];
            for( int k=0;k<col;k++ )
            {
                mat[i][k] = 0;
            }
        }
    }
};

matrix* MultiplyMat(matrix* matA,matrix* matB)
{
    matrix* tMat = new matrix(matA->row,matB->col);
    if(matA->row == matB->col)
    {
        for( int i=0; i<matA->row; i++ )
        {
            for( int j=0;j<matB->col;j++ )
            {   
                for( int m=0;m<matB->col;m++ )
                {
                    tMat->mat[j][i] += matA->mat[j][m] * matB->mat[m][i];
                }
            }
        }
    }

    return tMat;
}

void PrintMatrix(matrix* tMat)
{
    cout<<"Print: Matrix\n\n";
    for( int i=0;tMat->row;i++ )
    {
        for( int j=0;j<tMat->col;j++ )
        {
            cout<<" "<<tMat->mat[i][j];

        }
        cout<<"\n";
    }

}

int main()
{
    matrix matB(3,4);
    matrix matA(4,3);

    matA.mat[0][0] = 2;
    matA.mat[0][1] = 1;
    matA.mat[0][2] = 4;
    matA.mat[1][0] = 6;
    matA.mat[1][1] = 5;
    matA.mat[1][2] = 9;
    matA.mat[2][0] = 8;
    matA.mat[2][1] = 7;
    matA.mat[2][2] = 11;
    matA.mat[3][0] = 5;
    matA.mat[3][1] = 5;
    matA.mat[3][2] = 9;

    matB.mat[0][0] = 2;
    matB.mat[0][1] = 1;
    matB.mat[0][2] = 4;
    matB.mat[0][3] = 3;
    matB.mat[1][0] = 6;
    matB.mat[1][1] = 5;
    matB.mat[1][2] = 9;
    matB.mat[1][3] = 12;
    matB.mat[2][0] = 8;
    matB.mat[2][1] = 7;
    matB.mat[2][2] = 11;
    matB.mat[2][3] = 13;

    matrix* matC = MultiplyMat(&matA,&matB);
    PrintMatrix(matC);


    return 0;
}

I am just trying to multiplay two matrices, the g++ compiler gives error "segmentation fault" I have tried debuging method(found on this site) but failed to remove the error!

Any help?

This line is wrong:

matrix* tMat = (matrix*)malloc(sizeof(matrix));

I'm not entirely sure what you are expecting this to do, but it probably doesn't do that... In fact, it doesn't do much at all, other than create a block of memory large enough for a the struct matrix . It is filled with some random garbage (which may or may not be zeros).

You then go on and use it:

                tMat->mat[j][i] += matA->mat[j][m] * matB->mat[m][i];

which most likely means you are either accessing NULL or some random garbage address that isn't valid. You then return the pointer to it, which is not freed here:

matrix* matC = MultiplyMat(&matA,&matB);
PrintMatrix(matC);

return 0;

You probably want something like:

matrix* tMat = new matrix(matB->col, matA->row);

But you would be much better off creating a matrix operator*(const matrix& a, const matrix& b) , so you don't return a pointer at all. The overhead will be pretty small.

Your matrix actually has a constructor but it isn't called when allocation memory using malloc() . You clearly want to use

matrix* tMat = new matrix(m, n);

with suitable argument m and n . Well, actually, you rather want to use

std::unique_ptr<matrix> tMat(new matrix(m, n));

... but this is just related to the next problem you'll get once you get past your segmentation fault: you also need to clean up resources. It is also not quite what your really want, though, because you really want something like this:

matrix MultiplyMat(matrix const& a, matrix const& b) {
     // ...
     matrix result(m, n);
     // ...
     return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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