Jump to content
  • Advertisement
Sign in to follow this  
EmptyVoid

Does Delete actually do anything?

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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];
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
ptr = NULL is not a general solution, since there may be other pointers to the deallocated memory. Hurray for automatic memory management!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!