sage45
10-14-2005, 10:44 PM
I'm having a problem when trying to delete the allocated memory for a class using inheritance...
When declared as:
// in main
Base **BasePtr; // Pointer to a pointer to a class called Base. Base is a pure virtual class.
// after reading in ArraySize
BasePtr = new Base*[ArraySize]; // Make a new array of pointers
for (i = 0; i < ArraySize; i++)
BasePtr[i] = NULL;
char switch_var;
inFile >> switch_var;
switch(switch_var)
{
case 'S': BasePtr[index] = new Child1; // create new instance of Child1 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child1 implementation.
break;
case 'T': BasePtr[index] = new Child2; // create new instance of Child2 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child2 implementation.
break;
case 'P': BasePtr[index] = new Child3; // create new instance of Child3 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child3 implementation.
break;
default: cerr << "Bad data in input file." << endl;
exit(1);
}I would assume then that I could simply deallocate the memory as such:for (i = 0; i < ArraySize; i++)
delete[] *BasePtr;
delete[] BasePtr;This however does not work, I get an assertion error everytime. So then I tried this way:for (int k = 0; k < ArraySize; k++)
delete[] BasePtr[k]; // delete each individual charge
delete[] BasePtr; // delete array of pointers to chargeAnd again I get an assertion error. From what I understand, an assertion error happens when you try to deallocate memory that you do not own, however, I know that I own this memory because I can do the following:for (int i = 0; i < ArraySize; i++)
{
cout << someVirtualChildFunction();
}And I get the correct output from that memory location... How can I deallocate this memory???
Thanks,
-sage-
When declared as:
// in main
Base **BasePtr; // Pointer to a pointer to a class called Base. Base is a pure virtual class.
// after reading in ArraySize
BasePtr = new Base*[ArraySize]; // Make a new array of pointers
for (i = 0; i < ArraySize; i++)
BasePtr[i] = NULL;
char switch_var;
inFile >> switch_var;
switch(switch_var)
{
case 'S': BasePtr[index] = new Child1; // create new instance of Child1 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child1 implementation.
break;
case 'T': BasePtr[index] = new Child2; // create new instance of Child2 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child2 implementation.
break;
case 'P': BasePtr[index] = new Child3; // create new instance of Child3 class.
BasePtr[index] -> input(inFile); // call virtual input function, inheritance will call the Child3 implementation.
break;
default: cerr << "Bad data in input file." << endl;
exit(1);
}I would assume then that I could simply deallocate the memory as such:for (i = 0; i < ArraySize; i++)
delete[] *BasePtr;
delete[] BasePtr;This however does not work, I get an assertion error everytime. So then I tried this way:for (int k = 0; k < ArraySize; k++)
delete[] BasePtr[k]; // delete each individual charge
delete[] BasePtr; // delete array of pointers to chargeAnd again I get an assertion error. From what I understand, an assertion error happens when you try to deallocate memory that you do not own, however, I know that I own this memory because I can do the following:for (int i = 0; i < ArraySize; i++)
{
cout << someVirtualChildFunction();
}And I get the correct output from that memory location... How can I deallocate this memory???
Thanks,
-sage-