Hello, I am runing this program this works if I dont delete anything in destructor. If I use delete variable name in destructor then it creates problem. It creates problem when destrctor get called while operator ovrerloading. I think It is trying to delete the memory that has already been deleted.
COuld any one help me in solving tis issue?

#include
<iostream> 
using
namespace std; 
class
matrix 
{
int **ptr,row,col; 
public
: 
matrix()
{
row = 2;
col = 2;
ptr = 
new int *[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++) 
ptr[i][j] = 0 ;
}
cout << 
"Non parametrized constructor invoked" << endl; 
}
matrix(
int m, int n) 
{
row = m ;
col = n;
ptr = 
new int *[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++){ 
ptr[i][j] = 0 ;
}
}
cout << 
"Parametrized constructor invoked" << endl; 
}
~matrix()
{
for (int i=0;i<row ; i++) 
{
delete ptr[i]; 
} 
delete ptr; 
cout << 
"Destructor invoked" << endl; 
}
void getdata() 
{
cout << 
"This matrix has " << row << "rows and" << col << "colums" << endl; 
cout << 
"Enter the array elements: " << endl; 
ptr = 
new int*[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++) 
cin >> ptr[i][j] ;
}
}
void output() 
{
cout << 
"Matrix is: " << endl; 
for(int i=0;i<row;i++) 
{
for(int j=0;j<col;j++) 
{
cout << ptr[i][j] << 
"\t"; 
}
cout << endl;
}
}
// + operator overloading 
matrix 
operator + ( matrix obj) 
{
matrix obj1;
for (int i=0;i<row;i++) 
{ 
for (int j=0;j<col;j++) 
{
obj1.ptr[i][j] = ptr[i][j] + obj.ptr[i][j];
}
}
return obj1; 
}
void mem() 
{
cout << 
"Memory layout is: " << endl; 
for(int i=0;i<row;i++) 
{
for(int j=0;j<col;j++) 
{
cout << ptr[i] << 
"\t"; 
}
cout << ptr << 
"\t"; 
cout << endl;
}
}
};



void main() 
{
matrix mat, mat1(2,2),mat2(2,2) ;
mat1.getdata();
mat2.getdata();
mat = mat1 + mat2; 
cout << 
"Bye" << endl; 
}

Param

Edited 6 Years Ago by Nick Evan: Added code-tags

COuld any one help me in solving tis issue?

If you had posted the code using code tags, the code would probably be readable. Now it is not. Perhaps see
What are code tags?.

Comments
It's too unreadable for me to even care, even with code tags

Is that ok now...

#include
<iostream> 
using
namespace std; 
class
matrix 
{
int **ptr,row,col; 
public
: 
matrix()
{
row = 2;
col = 2;
ptr = 
new int *[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++) 
ptr[i][j] = 0 ;
}
cout << 
"Non parametrized constructor invoked" << endl; 
}
matrix(
int m, int n) 
{
row = m ;
col = n;
ptr = 
new int *[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++){ 
ptr[i][j] = 0 ;
}
}
cout << 
"Parametrized constructor invoked" << endl; 
}
~matrix()
{
for (int i=0;i<row ; i++) 
{
delete ptr[i]; 
} 
delete ptr; 
cout << 
"Destructor invoked" << endl; 
}
void getdata() 
{
cout << 
"This matrix has " << row << "rows and" << col << "colums" << endl; 
cout << 
"Enter the array elements: " << endl; 
ptr = 
new int*[row]; 
for(int i=0;i<row;i++) 
{
ptr[i] = 
new int[col]; 
for(int j=0;j<col;j++) 
cin >> ptr[i][j] ;
}
}
void output() 
{
cout << 
"Matrix is: " << endl; 
for(int i=0;i<row;i++) 
{
for(int j=0;j<col;j++) 
{
cout << ptr[i][j] << 
"\t"; 
}
cout << endl;
}
}
// + operator overloading 
matrix 
operator + ( matrix obj) 
{
matrix obj1;
for (int i=0;i<row;i++) 
{ 
for (int j=0;j<col;j++) 
{
obj1.ptr[i][j] = ptr[i][j] + obj.ptr[i][j];
}
}
return obj1; 
}
void mem() 
{
cout << 
"Memory layout is: " << endl; 
for(int i=0;i<row;i++) 
{
for(int j=0;j<col;j++) 
{
cout << ptr[i] << 
"\t"; 
}
cout << ptr << 
"\t"; 
cout << endl;
}
}
};



void main() 
{
matrix mat, mat1(2,2),mat2(2,2) ;
mat1.getdata();
mat2.getdata();
mat = mat1 + mat2; 
cout << 
"Bye" << endl; 
}

Edited 6 Years Ago by Nick Evan: Changed [\code] in [/code]

This article has been dead for over six months. Start a new discussion instead.