RunTime error when accessing a dynamic memory allocated in class

Hello. Following is a code in which I have implemented matrices using classes and then added several functions into the class, such as that of addition and subtraction... To me, the code looks ok and it works fine as well until the input function..
However, it started giving error when I tried to call addition function (while accessing the data from the matrices)... I added a copy constructor, hence and that solved the problem.
But then, when I called the display function for the new matrix, which I made for the result of the addition function, it again gave the same runtime error when it tried to access the object's data.. Can anyone tell me what's the issue in it?
To be brief, it is runtime error when I call the display function for the Object C (in int main).
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
#include <iostream>
using namespace std;
class matrix
{
int** ptr;
int cols;
int rows;
public:
matrix();
matrix(int, int);
matrix addmatrix(matrix);
bool check_size(matrix);
matrix submatrix(matrix);
void displaymatrix();
void inputmatrix();
matrix(matrix&);
~matrix();
};
matrix::matrix()
{
rows = cols = 2;
ptr = new int*[2];
for (int i = 0; i < 2; i++)
ptr[i] = new int[2];
}
matrix::matrix(matrix& obj)
{
this->rows = obj.rows;
this->cols = obj.cols;
this->ptr = new int* [rows];
for (int i = 0; i < this->rows; i++)
this->ptr[i] = new int[this->cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
this->ptr[i][j] = obj.ptr[i][j];
}
}
matrix::matrix(int a, int b)
{
rows = a;
cols = b;
ptr = new int* [a];
for (int i = 0; i < a; i++)
ptr[i] = new int[b];
}
void matrix::displaymatrix()
{
cout << "Matrix: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << this->ptr[i][j] << " ";
cout << endl;
}
cout << endl;
}
void matrix::inputmatrix()
{
cout << endl << "Enter Matrix Elements" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cin >> this->ptr[i][j];
cout << endl;
}
}
matrix matrix::addmatrix(matrix a)
{
matrix C(this->rows, this->cols);
if (check_size(a))
{
for (int i = 0; i < a.rows; i++)
{
for (int j = 0; j < a.cols; j++)
{
C.ptr[i][j] = (this->ptr[i][j]) + (a.ptr[i][j]);
}
}
}
return C;
}
matrix matrix::submatrix(matrix a)
{
if (check_size(a))
{
for (int i = 0; i < this->rows; i++)
{
for (int j = 0; j < this->cols; j++)
{
ptr[i][j] = ptr[i][j] - a.ptr[i][j];
}
}
}
return *this;
}
matrix::~matrix()
{
for (int i = 0; i < this->rows; i++)
delete[]ptr[i];
delete[]ptr;
}
bool matrix::check_size(matrix a)
{
if (this->rows != a.rows || this->cols != a.cols)
return false;
return true;
}
int main()
{
matrix A (3, 3);
matrix B(3, 3), C(3, 3);
A.inputmatrix();
B.inputmatrix();
A.displaymatrix();
B.displaymatrix();
C = A.addmatrix(B);
C.displaymatrix();
matrix* arr;
arr = new matrix[3];
arr[0].inputmatrix();
arr[1].inputmatrix();
arr[2] = arr[0].addmatrix(arr[1]);
arr[2].displaymatrix();
system("pause");
return 0;
}
> A.inputmatrix();
if your code needs user input, then provide an example


> C = A.addmatrix(B);
that calls the assignment operator, which will do a shallow copy and then you'll delete the same pointer twice.
you need to provide a custom assignment operator as well.


by the way, you forgot to delete[] arr
Last edited on
You haven't added a copy constructor. The copy constuctor signature is:
matrix(const matrix &);

Yours doesn't have a const, and so the compiler is probably using the default, which in your case is the wrong thing.
Topic archived. No new replies allowed.