Random crashes

closed account (y0XSE3v7)
The code is supposed to take two matrices from txt files, multiply them and display the result, it works but most of the time it crashes after displaying the result. Can anyone tell me whats wrong, is it the dynamic memory?

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
#include <iostream>
#include <cmath>
#include <fstream>

class matrix
{
    public:
    int rows;
    int columns;
    double ** E;
    double * & operator [] (int i)
    {
        return (E[i]);
    }
    matrix (int r,int c);
    ~matrix ();

    //Matrix algebra
    matrix operator * (matrix A);
    matrix operator + (matrix A);
    matrix operator - (matrix A);

    //Display matrix on console
    void Display();
};

matrix::matrix (int r,int c)
{
    rows = r;
    columns = c;
    E = new double * [r];
    for (int i=0;i<rows;i++)
    {
        E[i] = new double [columns];
    }
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
        {
            E[i][j]=0;
        }
    }
}
matrix::~matrix ()
{
    for (int i=0;i<rows;i++)
    {
        delete [] E[i];
    }
    delete [] E;
}

void get_matrix(std::fstream &data, matrix &A);

int main()
{
    std::fstream data;
    matrix A(3,4);
    matrix B(4,4);

    data.open("A.txt",std::ios::in);
    get_matrix(data,A);
    data.close();

    data.open("B.txt",std::ios::in);
    get_matrix(data,B);
    data.close();

    matrix C = A*B;
    C.Display();
}

//Matrix operations
matrix matrix::operator * (matrix A)
{
    matrix B (rows,A.columns);
    double d=0;
    if (columns!=A.rows) std::cout<<"Matrix dimensions don't match!"<<'\n';
    else
    {
        for (int i=0;i<B.rows;i++)
        {
            for (int j=0;j<B.columns;j++)
            {
                for (int k=0;k<columns;k++)
                {

                        B[i][j] += E[i][k]*A[k][j];
                }

            }
        }
        return (B);
    }
}

//Display matrix
void matrix::Display ()
{
    std::cout<<'\n';
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
            {
                std::cout<<" "<<E[i][j];
            }
    std::cout<<'\n';
    }
}

//Input matrix from a txt
void get_matrix(std::fstream &data, matrix &A)
{
    for (int i=0;i<A.rows;i++)
    {
        for (int j=0;j<A.columns;j++)
            {
                data>>A[i][j];
            }
    }
}
You did not provide a copy constructor, so compiler generated one for you. It copies elements one-by-one, including pointers. So in line matrix C = A*B; C will contain same pointers as unnamed temporary returned by multiplication. After that line temporary is destroyed, freeing memory. After that access to C elememts creates UB. It might not crash right away, but destructor will try to delete memoty again, leading to double delete and high probability of crash.
Implement copy constructor and copy assignment. (The "rule of three.")
closed account (y0XSE3v7)
Thanks guys, I solved it. Should I post the solved version of the code in case someone new to programming like me makes the same mistake?
Topic archived. No new replies allowed.