Why do I keep getting a core dump

So my professor gave me a matrix class some time back and I decided to start messing around with it, to give it new functions for addition and multiplication, but I've hit an odd snag. I'm just getting started on the multiplication and I know that if you multiply a row vector by a column vector it's supposed to produce only a single value that can later be used as a scaler if you wanted. Since the return value for this particular function is a matrix I decided I wanter to try and get around that by making it by having that particular situation return a 1x1 matrix that would act as an int, and then I wanted to add the feature so that should my function receive a 1x1 matrix it treats it like a scaler. But for some reason. Thats the code I'm working on right now. But anytime I try and run it I get a message that read Segmentation Fault: Core Dumped, which if memory serves right means that the calculation would have ran for ever and to stop itself ended the program... At least thats what another prof told me. Anyway I'm really not sure what the issue is since the code it a copy paste of my working scaler multiplication function with a few light changes. Any help is appreciated.

Update: I added the code meant to multiply the row by the column and same core dump issue. I don't see any endless loops so... I'm quite confused.

Update: Since I was asked to clarify, all of the issues appear in matMul(mat a, mat b). All the other functions work perfectly fine, including matMul(mat a, int s)
Matrix class header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <random>
#include <iomanip>
#include <cmath>
#include <ctime>
class mat
{
public:
    mat(){A=NULL;}
    ~mat(){}


    mat(int r, int c){row=r; col=c;         ///Build an empty matrix of row r and column c
                    A=new double *[row];
                    for(int i=0; i<row; i++)
                    {
                        A[i]=new double[col];
                    }
                    for(int i=0; i<row; i++)
                    {
                        for(int j=0; j<col; j++)
                        {
                            A[i][j]=0.0;
                        }
                    }
            }


            int getrow(){return row;}
            int getcol(){return col;}     ///Retrieve the needed info
            //int getValue(int r, int c)
            //{
            //cout << A[r][c] << endl;
            //return A[r][c];
            //}


            void display()
            {
                for(int i=0; i<row; i++)
                {
                    for(int j=0; j<col; j++)
                    {
                        cout<<setw(8)<<A[i][j];
                    }
                    cout<<endl;
                }
                cout<<endl;
            }



        void ident(){  ///convert the current matrix into an indentity matrix
                    if(col==1){A[0][0]=1.0;}
                    else{
                    for(int i=0; i<row; i++)
                    A[i][i]=1.0;
                    }
                }

        void random_mat(double m)   ///Fill the matrix with random values
        {   double sum=0.0;
            double r;
            default_random_engine gen(time(NULL));
            uniform_real_distribution<double> rdist(-1*m, m);
            for(int i=0; i<row; i++)
            {
                for(int j=0; j<col; j++)
                {
                    r=rdist(gen);
                    A[i][j]=r;
                    sum = sum+abs(r);
                }
                A[i][i]=sum;
                sum=0.0;
            }
        }


mat matAdd(mat &a, mat &b) ///add two matrices to each other
    {
        int bRow = b.getrow();
    int bCol = b.getcol();
    mat c(bRow,bCol);
        for(int i=0; i<bRow; i++)
        {
            for(int j=0; j<bCol; j++)
            {
                c.A[i][j] = a.A[i][j]+b.A[i][j];
            }
            }
        return c;
    }

    mat matSub(mat &a, mat &b) ///subtract two matrices to each other
{
    int bRow = b.getrow();
    int bCol = b.getcol();
    mat c(bRow,bCol);
        for(int i=0; i<bRow; i++)
        {
            for(int j=0; j<bCol; j++)
            {
                c.A[i][j] = a.A[i][j]-b.A[i][j];
            }
            }
        return c;
}

mat matMul(mat a, int s) ///multiply a matrix and a scaler
{
    int aRow = a.getrow();
    int aCol = a.getcol();
    mat c(aRow,aCol);
        for(int i=0; i<aRow; i++)
        {
            for(int j=0; j<aCol; j++)
            {
                c.A[i][j] = a.A[i][j] * s;
            }
        }
        return c;
    }


mat matMul(mat a, mat b)
{

int aRow = a.getrow();
int aCol = a.getcol();
int bRow = b.getrow();
int bCol = b.getcol();

if (aRow == 1 && aCol == 1)             /// If matrix a has only one value treat it like a scaler to b
    {

           mat c(bRow,bCol);
        for(int i=0; i<bRow; i++)
        {
            for(int j=0; j<bCol; j++)
            {
                c.A[i][j] = b.A[i][j] * a.A[1][1];
            }
        }
        return c;
    }

    else if (bRow == 1 && bCol == 1)     ///if matrix b only has one value, treat it like a scaler to a
    {
           int tempA = a.getrow();
           int tempB = a.getcol();
           mat c(aRow,aCol);
        for(int i=0; i<aRow; i++)
        {
            for(int j=0; j<aCol; j++)
            {
                c.A[i][j] = a.A[i][j] * b.A[1][1];
            }
        }
        return c;
    }

    else if (aRow == 1 && bCol == 1 && aCol == bRow)///if a row is multiplied by a column, get a single number
        {
          mat c(1,1);
          int sum = 0;
        for(int i=0; i<aCol; i++)
        {

                sum = sum + a.A[1][i] * b.A[i][1];
        }
        c.A[1][1] = sum;
        return c;
        }

}






private:
    int row;
    int col;
    double ** A;
};





Test cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>

using namespace std;
#include "matrix.h"





int main()
{   int r,c;
cout<<"enter rows and cols"<<endl;
//cin>>r>>c;
    mat A(1,3);
    A.random_mat(5);
    A.display();

    mat B(3,1);
    B.random_mat(3);
    B.display();

    mat C(1,1);
   C = C.matMul(A,B);
    C.display();
    return 0;
}
Last edited on
Yatora: What? I'm not using the site as a code dump? I'm asking a simple question and providing the code so someone can help me figure out why I keep getting a core dump
Last edited on
most likely you have an array out of bounds.
figure out which functions work and which one crashes it.
then look deeper into that one, or provide more info here.
Sorry I guess I missed that part. All of the functions work except for matMul(mat a, mat b)

Every time I try to call upon that function is when I get the Segmentation Fault: Core Dump message. It also occurs no matter which of the if statements I try to use.
I figured it out, basic mistake. I was calling on location (1,1) in a matrix that only has a position (0,0)
Topic archived. No new replies allowed.