Function returning classes
Apr 27, 2013 at 2:31pm UTC
Dear all,
I have a problem with a function that returns a class defined by myself. The code is compiled without problems, but program crashes, for a reason I cannot understand.
I report the code below. The function that is troubling me is named "catV". Could anyone help me in understanding what is wrong?
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
#include <iostream>
using namespace std;
class mtx
{
public :
int nr, nc;
double * data;
mtx(); //default constructor
mtx(int , int ); //constructor
mtx(mtx&); //copy constructor
~mtx() {delete [] data;} //destructor
void display();
};
mtx::mtx()
{
nr = 0;
nc = 0;
data = NULL;
}
mtx::mtx(int n_rows, int n_cols)
{
nr = n_rows;
nc = n_cols;
data = new double [nr*nc];
for (int i=0; i<nr*nc; i++)
data[i] = 0;
}
mtx::mtx(mtx& M)
{
nr = M.nr;
nc = M.nc;
for (int i=0; i<nr*nc; i++)
data[i] = M.data[i];
}
void mtx::display()
{
cout << endl;
int j=0;
for (int i=0; i<nr; i++)
{
for (int k=0; k<nc; k++)
cout << data[j++] << '\t' ;
cout << endl;
}
cout << endl;
return ;
}
mtx catV(mtx A, mtx B)
{
if (A.nc == B.nc)
{
mtx C(A.nr+B.nr, A.nc);
/*int QA = A.nr*A.nc;
int QB = B.nr*B.nc;
for (int i=0; i<QA; i++) C.data[i] = A.data[i];
for (int i=0; i<QB; i++) C.data[i+QA] = B.data[i];*/
return C;
}
else
{
mtx C;
return C;
}
}
int main()
{
mtx A(4,3), B;
A.display();
B = A;
for (int i=0; i < 12; i++) B.data[i] = 12;
B.display();
mtx C;
C = catV(A,B);
//C.display();
return 0;
}
I thank you in advance.
Panecasareccio.
Apr 27, 2013 at 2:55pm UTC
You did not allocate memory for data member data in your copy constructor
1 2 3 4 5 6 7
mtx::mtx(mtx& M)
{
nr = M.nr;
nc = M.nc;
for (int i=0; i<nr*nc; i++)
data[i] = M.data[i];
}
The value of data is undefined.
Last edited on Apr 27, 2013 at 2:56pm UTC
Apr 27, 2013 at 3:12pm UTC
You're not always allocating memory for your pointer. Check your copy constructor to insure it properly re-allocates the proper amount of memory for the copy.
Apr 27, 2013 at 3:43pm UTC
Thank you all.
you were right, I had not allocated memory doe the array data in my copy constructor. Now I have changed the code accordingly, it runs well, and I get the desired output.
However, something even more weird happens: I run the executable from a command line (windows 8), the program runs correctly. Then, if I run it again, it gets stuck, like it was in an infinite loop. I cannot understand why.
The new code is reported below. could anyone please take a look at that?
My felt thnks.
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
#include <iostream>
using namespace std;
class mtx
{
public :
int nr, nc;
double * data;
mtx(); //default constructor
mtx(int , int ); //constructor
mtx(mtx&); //copy constructor
~mtx() {delete [] data;} //destructor
void display();
};
//default constructor
mtx::mtx()
{
nr = 0;
nc = 0;
data = NULL;
}
//constructor: accepts size, sets data to zeros
mtx::mtx(int n_rows, int n_cols)
{
nr = n_rows;
nc = n_cols;
data = new double [nr*nc];
for (int i=0; i<nr*nc; i++)
data[i] = 0;
}
//copy constructor
mtx::mtx(mtx& M)
{
nr = M.nr;
nc = M.nc;
data = new double [nr*nc];
for (int i=0; i<nr*nc; i++)
data[i] = M.data[i];
}
//diaplay a matrix on the screen
void mtx::display()
{
cout << endl;
int j=0;
for (int i=0; i<nr; i++)
{
for (int k=0; k<nc; k++)
cout << data[j++] << '\t' ;
cout << endl;
}
cout << endl;
return ;
}
mtx catV(mtx A, mtx B)
{
if (A.nc == B.nc)
{
mtx C(A.nr+B.nr, A.nc);
int QA = A.nr*A.nc;
int QB = B.nr*B.nc;
for (int i=0; i<QA; i++) C.data[i] = A.data[i];
for (int i=0; i<QB; i++) C.data[i+QA] = B.data[i];
return C;
}
else
{
mtx C;
return C;
}
}
int main()
{
mtx A(4,3), B(4,3);
A.display();
for (int i=0; i < 12; i++) B.data[i] = 12;
B.display();
mtx C;
C = catV(A,B);
C.display();
return 0;
}
Apr 27, 2013 at 3:56pm UTC
Here you are using the copy assignment operator
C = catV(A,B);
But you shall explicitly define this operator the same way as the copy constructor because you need to manipulate allocated memory.
Last edited on Apr 27, 2013 at 3:56pm UTC
Topic archived. No new replies allowed.