Delete a Pointer in an Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void CParticleSystem::RemoveParticle(int index)
{
	int ct;
	npart--;
	CParticle **save = new CParticle*[npart];
	for (ct = 0; ct < index; ct++)
	{
		save[ct] = particles[ct];
	}
	for (ct = index; ct < npart; ct++)
	{
		save[ct] = particles[ct + 1];
	}
	delete particles[index];
	delete[] particles;
	particles = save;
}

void CParticleSystem::Render()
{
	int ct;
	for (ct = 0; ct < npart; ++ct)
	{
		if(ct==npart)
			return;
		if(!particles[ct]->Render())
		{
			RemoveParticle(ct);
			ct--;
		}
	}
}


npart holds how many elements are in the array.
Render() returns FALSE if it no longer needs to be rendered.

With that code it crashes when RemoveParticle() is called. For some reason if I remove the line delete particles[index]; it works.

I am writing code for the Zii EGG so I don't get the error message and the debugging tool is currently broken so it's been trial and error.

From what I've found online this is how you could free that RAM but like I said it crashes. Any idea?

Thanks a lot,
JR
Hmmm....

Try this code if you can understand it...

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 = new char*[xDim];
    for(int i = 0; i < xDim; i++){
        myArray[i] = new char[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;
}


Hope this helps.
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.

Thanks,
JR

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.
I really appreciate the help but I don't think you understand what I am trying to do.

That code would delete all the elements of my array, not just one.

Correct me if I am wrong but I think delete[] is only for arrays allocated with new[] or new type[x], not a single pointer like I have.

Is there an easier way to manage a bunch of instances of a class?
Topic archived. No new replies allowed.