destructor problem

T

Tony Johansson

Hello Experts!!

I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.

I know I must have a copy constructor but I have just cut out that just to
make it easier for you to read.

Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem

Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically memory works fine.

Now to my problem here I have written a destructor that I was sure should
work but it doesn't.
Why does not my destructor work because as I believe I have a pointer in
matris[number of rows]to allocated
dynamically memory.

Can you tell me what the problem is?
~Matris() //Destruktor
{
for (int i=0; i< c_rader; i++)
delete matris; //********** Here I get an Debug
assertion failed *************

delete *matris;
}

Many thanks
//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"

int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};

//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
matris = new Intvektor*[0];
}

Matris(int rows, int cols) : c_rows(rows), c_columns(cols)
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};
 
H

Howard

Tony Johansson said:
Hello Experts!!


Can you tell me what the problem is?
~Matris() //Destruktor
{
for (int i=0; i< c_rader; i++)
delete matris; //********** Here I get an Debug
assertion failed *************


delete [] matris;

Wherever you use the form "new type[size]", you need to use the form "delete
[] pointer".
delete *matris;
}

Many thanks
//Tony

//Here in main
//**********
#include "intvektor.h"
#include "matris.h"

int main()
{
Matris a(2,2);
return 0;
}

//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];

Not a good idea. What if size = 0? If you're not going to allocate any
items, you should just set the pointer to NULL.
}

Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};

//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
matris = new Intvektor*[0];


Why are you trying to allocate an array of ZERO pointers? Why not just set
matris to NULL?
}

Matris(int rows, int cols) : c_rows(rows), c_columns(cols)
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};


-Howard
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top