I am trying to get this code down for a class and deals with matrix but I keep getting the error:
malloc: *** error for object 0x7f9edf504080: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
If anybody could help, it will be appreciated.
#include <iostream>
using namespace std;
class matrix {
int **a;
public:
matrix() {
int i,j;
a = new int*[3];
for (i = 0; i <3; i++)
a[i] = new int[3];
cout << "Enter elements for 3x3 matrix: \n";
for (i = 0;i <3; i++)
for (j = 0;j <3; j++)
cin >> a[i][j];
}
matrix (matrix &x) {
int i,j;
a = new int*[3];
for (i = 0;i <3;i++)
a[i] = new int[3];
for (i = 0;i < 3; i++)
for (j = 0;j < 3; j++)
a[i][j] = x.a[i][j];
}
~matrix() {
int i;
for (i = 0;i<3;i++)
delete a;
}
void putinmatrix();
};
void matrix::putinmatrix() {
int i,j;
for (i = 0;i < 3;i++) {
for(j = 0;j<3;j++)
cout << a[i][j] <<" ";
cout<<endl;
}
}
int main() {
matrix obj1;
matrix obj2(obj1);
cout<<"Matrix 1 and Matrix 2:\n";
obj1.putinmatrix();
}