Deletion of a matrix

H

Huibuh

In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter



class matrix

{

public:

matrix* next;

int xDim;

int yDim;

int **field;

~ matrix()

{

for (int line=0; line<xDim; line++)

delete [] field[line];

delete [] field;

// delete next;

};

matrix::matrix(int x, int y)

{

int i, j;



xDim=x;

yDim=y;

field = new int* [xDim];

for(i=0; i<xDim; i++)

field = new int [yDim];

for(i=0; i<xDim; i++)

for(j=0; j<yDim; j++)

field[j]=INT_MAX;

}

void Setnext (matrix* ne) {next=ne;}

matrix* Getnext () {return next;}

void Setmatrix (int Lines, int Splits, int w)

{

field[Lines][Splits]=w;

}

int Getmatrix (int Lines, int Splits)

{

return field[Lines][Splits];

}

};
 
V

Victor Bazarov

Huibuh said:
In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter
[...]

You didn't post any code that shows how you use your matrix. So, I am
guessing here, but most likely you didn't follow the "Rule of Three".
Read about the Rule of Three in the FAQ or in the newsgroup archives
(http://groups.google.com)

V
 
J

John Harrison

Huibuh said:
In one of my header-files I have a class named "matrix" with the function
to
construct a matrix. It works properly but at the time of destruction of
the
class the program stops. What have I done wrong? Thanks in advance Dieter

You've failed to define either a copy constructor or an assignment operator
when for your class they are desperately needed. Look these up in your
favourite C++ book.

john
 
O

Old Wolf

Huibuh said:
In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter

In your destructor you have the line:
// delete next;

This line (if it were not commented out) is a problem
because you never assign 'next' in your constructor.
(I think you wanted next = NULL there).

In fact this class is badly designed because you are mixing
the matrix with the implementation of a matrix container.
If you ever change your container requirements (eg. if you
want reverse linking) then you have to change the matrix
class. Instead you should have the matrix and the container
separate.

You could avoid all your problems by using standard containers
and memory management:

class matrix
{
std::vector< std::vector<int> > field;
etc.

and then use std::list said:
};

matrix::matrix(int x, int y)

{

int i, j;



xDim=x;

yDim=y;

field = new int* [xDim];

for(i=0; i<xDim; i++)

field = new int [yDim];

for(i=0; i<xDim; i++)

for(j=0; j<yDim; j++)

field[j]=INT_MAX;

}

void Setnext (matrix* ne) {next=ne;}

matrix* Getnext () {return next;}

void Setmatrix (int Lines, int Splits, int w)

{

field[Lines][Splits]=w;

}

int Getmatrix (int Lines, int Splits)

{

return field[Lines][Splits];

}

};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top