473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destructor problem

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[i]; //********** 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[i] = v.array[i];
}
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[i] = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};


Jul 23 '05 #1
1 1519

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:Yw*******************@newsb.telia.net...
Hello Experts!!
Can you tell me what the problem is?
~Matris() //Destruktor
{
for (int i=0; i< c_rader; i++)
delete matris[i]; //********** Here I get an Debug
assertion failed *************
delete [] matris[i];

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[i] = v.array[i];
}
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[i] = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};


-Howard
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
1827
by: bArT | last post by:
Hi! I have a problem with such situation. I have a class like below and have some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor and I free in destructor. For one pointer...
9
3645
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
26
3939
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
7940
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
5258
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
35
3267
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
23
2551
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
8
2022
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
10
459
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
7
2122
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.