Basically, I have a double pointer that points to a list of pointers that point to instances of a class and I need to remove one of the instances in the middle of the array. Here is what my code looks like.
These codes are from Professional C++ 2005 book by Solter and Kleper
1 2 3 4 5 6 7 8 9 10 11 12
//proper way of allocating memory for a two-dimensional array
char** allocateMem(int xDim, int yDim)
{
int i;
//allocate first dimension
char** myArray = newchar*[xDim];
for(int i = 0; i < xDim; i++){
myArray[i] = newchar[yDim];
}
return myArray;
}
for deallocating
1 2 3 4 5 6 7 8
//proper way of deleting 2d arrray
void releaseMem(char** myArray, int xDim)
{
for(int i = 0; i < xDim; i++){
delete[] myArray[i];
}
delete[] myArray;
}
That code allocates an array of arrays, I have an array or pointers that point to one thing.
Also I need to remove one pointer in the array, not the whole thing.
What I was trying to do is to copy everything in particles into save except one pointer. Then free that pointer and make particles equal to save.
I have a feeling that it is copying the pointer I am trying to free but I don't see how. I'm going to mess with it a little now to see if I can figure it out.
Also I need to remove one pointer in the array, not the whole thing.
try this one, exclude the last part delete[] myArray;
1 2 3 4 5 6 7 8
//proper way of deleting 2d arrray
void releaseMem(char** myArray, int xDim)
{
for(int i = 0; i < xDim; i++){
delete[] myArray[i];
}
// delete[] myArray; <--don't include this I think??
}
But, since you use dynamic allocation, the problem now is to delete the entire array at the end after the object is used.
I am not an expert programmer and let's hope that some experts would post in your thread.