Dear DaniWebbers,
If you would all be so kind to help me, I have a problem here and I am trying to know why this happens...
In a large application I am working on, I need to have a struct of some sort with a sub struct-array and the sub struct-array has its own sub struct-array. All of this is dynamically allocated. This was previously done in C and typedef structs and the like, and I am trying to make it slightly more manageable by moving it all to C++ and classes. I'll make use of constructors and destructors, and also help myself by allocating the functions to their representative classes...
The problem happens when I try to free the memory. The code below shows the exact error I am having, but in a simple example. The provided code compiles and runs, but when it gets to the third iteration of 'delete' for mySub, it crashes... it seems as if when the first subclass is deleted, it deletes all the child classes of all the others in the array. Could that be possible?
If there is a better way to implement this, please let me know.
Thanx in advance
#include <string.h>
class SubSub {
public:
SubSub();
~SubSub();
char var1[30];
char var2[30];
int var3;
double var4;
};
class Sub {
public:
Sub();
~Sub();
SubSub * mySubSub;
char var1[30];
char var2[30];
int var3;
double var4;
int mySubSubCount;
};
class Super {
public:
Super();
~Super();
Sub * mySub;
char var1[30];
char var2[30];
int var3;
double var4;
int mySubCount;
};
//----Constructors
Super::Super()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
mySub = NULL;
mySubCount = 0;
}
Sub::Sub()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
mySubSub = NULL;
mySubSubCount = 0;
}
SubSub::SubSub()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
}
//----Destructors
Super::~Super()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
if (mySubCount > 0)
{
delete [] mySub;
}
mySubCount = 0;
}
Sub::~Sub()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
if (mySubSubCount > 0)
{
delete [] mySubSub;
}
mySubSubCount = 0;
}
SubSub::~SubSub()
{
strcpy(var1, "");
strcpy(var2, "");
var3 = 0;
var4 = 0.0;
}
void main()
{
int a = 3;
int b = 3;
int c = 3;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
Super * mySuper = new Super;
Sub * mySub;
SubSub * mySubSub;
do
{
for (i = 0; i < a; i++)
{
strcpy(mySuper->var1, "test1");
strcpy(mySuper->var2, "test2");
mySuper->var3 = 3;
mySuper->var4 = 4;
for (j = 0; j < b; j++)
{
if (mySuper->mySubCount > 0)
{
mySub = new Sub[mySuper->mySubCount + 1];
for (l = 0; l < mySuper->mySubCount; l++)
{
mySub[l] = mySuper->mySub[l];
}
delete [] mySuper->mySub;
mySuper->mySub = mySub;
delete mySub;
mySuper->mySubCount++;
}
else
{
mySuper->mySub = new Sub[1];
mySuper->mySubCount = 1;
}
strcpy(mySuper->mySub[j].var1, "test1");
strcpy(mySuper->mySub[j].var2, "test2");
mySuper->mySub[j].var3 = 3;
mySuper->mySub[j].var4 = 4;
for (k = 0; k < c; k++)
{
if (mySuper->mySub[j].mySubSubCount > 0)
{
mySubSub = new SubSub[mySuper->mySub[j].mySubSubCount + 1];
for (m = 0; m < mySuper->mySub[j].mySubSubCount; m++)
{
mySubSub[m] = mySuper->mySub[j].mySubSub[m];
}
delete [] mySuper->mySub[j].mySubSub;
mySuper->mySub[j].mySubSub = mySubSub;
delete mySubSub;
mySuper->mySub[j].mySubSubCount++;
}
else
{
mySuper->mySub[j].mySubSub = new SubSub[1];
mySuper->mySub[j].mySubSubCount = 1;
}
strcpy(mySuper->mySub[j].mySubSub[k].var1, "test1");
strcpy(mySuper->mySub[j].mySubSub[k].var2, "test2");
mySuper->mySub[j].mySubSub[k].var3 = 3;
mySuper->mySub[j].mySubSub[k].var4 = 4;
}
}
}
a--;
b--;
c--;
}
while (a && b && c);
}