Does Delete actually do anything?

Started by
9 comments, last by thedustbustr 15 years, 10 months ago
Well I've been wondering why all my memory does not get eaten up when make I make repeated calls to New and never call Delete. So what does Delete do? [Edited by - EmptyVoid on July 19, 2008 5:46:21 AM]
This is your life, and it's ending one minute at a time. - Fight club
Advertisement
delete frees the memory allocated by new. You *will* eat up all of your memory if you repeatedly call new (in a loop for example) without delete.

int main(){    while (true)    {        new int[1048576];    }}
Ra
delete releases the memory back to the free store.

Your method of profiling appears to be inaccurate. The task manager is not an accurate metric for these sorts of things.
I just realized I was using so little memory it was impossible to notice the change.
This is your life, and it's ending one minute at a time. - Fight club
using Delete also keeps from causing Memory leaks when using New.
Sometimes, after using delete, the pointers still contain addresses of the deallocated memory spaces. Using NULL after using delete helps you avoid this problem if you use the pointers later, so you don't access the wrong memory space or get any errors if you don't initialize them.

int *ptr;ptr = new int;*ptr = 10;delete ptr;ptr = NULL;


[Edited by - Moonshoe on July 16, 2008 10:33:39 PM]
Quote:Original post by Moonshoe
Sometimes, after using delete, the pointers still contain addresses of the deallocated memory spaces.

That's because you can't generally assume that the argument to delete is an lvalue (something that can be assigned to).

Oh and before the memory gets released, delete calls the destructor first.
ptr = NULL is not a general solution, since there may be other pointers to the deallocated memory. Hurray for automatic memory management!
Quote:Original post by Ahnfelt
ptr = NULL is not a general solution, since there may be other pointers to the deallocated memory. Hurray for automatic memory management!

If there are other pointers to the deallocated memory, then you shouldn't have called delete in the first place. The ptr = NULL is not the problem, that just prevents double deletion via the same pointer.
Quote:Original post by Moonshoe
Sometimes, after using delete, the pointers still contain addresses of the deallocated memory spaces. Using NULL after using delete helps you avoid this problem if you use the pointers later, so you don't access the wrong memory space or get any errors if you don't initialize them.


After calling delete on a pointer, the pointer normally still points at the deallocated memory. Technically, it's undefined what happens when you dereference this pointer, not simply because it points at freed space, but because the implementation could also change the value. However, the implementation normally doesn't, because there's no real reason to.

Stroustrup has claimed, IIRC, this was explicitly to allow the implementation to NULL the pointer automatically, and is disappointed that compiler vendors haven't by and large done so. However, that strikes me as bizarre, because (a) such behaviour is useless if not guaranteed in the Standard; and (b) properly designed code, as a rule, doesn't reuse the pointer, because the memory allocation is handled using RAII, and therefore the pointer that has just been deleted is part of an object whose lifetime has ended.

This topic is closed to new replies.

Advertisement