Delete object and its pointer

Hi!

I have been reading about how important it is to use the delete operator to avoid memory problems. However, I was wondering if it is possible to remove a pointer.

For example, imagine that I created a class cow. Imagine that I am using an array of pointers which point to objects of class cows (to all my 7 cows):

 
|*cow0|*cow1|*cow2|*cow3|*cow4|*cow5|*cow6|


if cow2 dies, I'd like to delete the object cow2 and move the subsequent pointers one position to the left. I'd use this code:

1
2
3
4
5
6
length_array--;
dead=2;
delete array_cows[dead];
for(int i=dead;i<length_array;i++){
array_cows[i]=array_cows[i+1];
}

After that, what I would have is:
 
|*cow0|*cow1|*cow3|*cow4|*cow5|*cow6|*cow6|


So, 2 questions which are somehow related:

1st - I don't want the cow6 to be replicated at the end. How could I delete that? Maybe doing:
1
2
3
4
5
array_cows[length_array] = null;

//or

array_cows[length_array]=0xcdcdcdcd;

???

2nd - the object cow2 was deleted but the pointer *cow2 is not deleted and it still exists (pointing to nowhere). How could I also delete it since I don't use it anymore and it is wasting memory(very little, I guess)?

Thank you in advanced
Last edited on
1st - I don't want the cow6 to be replicated at the end. How could I delete that?
You need to decrement 'length_array' and 'length' (if that's something different).
Line 1 should suffice. length_array=length_array--; // No need to assign

2nd - the object cow2 was deleted but the pointer *cow2 is not deleted and it still exists (pointing to nowhere)If you mean that the memory of the array is still the same: Yes. If you want a smaller array that reflects the actual size then you have to alloc the new (smaller) array and copy the pointer to it.

I'd recommend to use std::vector that does most of the work for you
Hi coder777,

1st -> Sorry, length_array was the same as length. I edited it to correct the mistake, now the code is correct!

2nd-> Yes, you are right, I chose a bad example. Imagine:
1
2
3
4
int *p=new int();
int a=10
p=&a;
delete p;

The object a was deleted. However, the pointer p still exists consuming memory (very little since it is just a direction of memory). Is it possible to delete it?
 
deletePointer p; //Deletes the pointer and not the object it points 

I hope now it is more clear ;)
No. If you REALLY need the small amount of memory, put it in a scope:

1
2
3
4
5
6
{
    int *p=new int();
    int a=10
    p=&a;
    delete p;
}//end scope so all automatic variables are removed 


But really, don't bother...it's probably not worth your time and the optimizer will mostly likely take advantage of stuff like that for you.
Ok thanks firedraco. What about my first question??
For your first question...

If you have allocated your original array on the heap, you can delete it and reallocate, but I would discourage that because it would be a waste of time.
If it was allocated statically, then you can't.
Topic archived. No new replies allowed.