Jump to content
  • Advertisement
Sign in to follow this  
Zakwayda

Does clear() release memory?

This topic is 5394 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

Hi - quick question. Let's say you have an octree leaf class which maintains a list<Entity*> indicating what entities are in that leaf that frame. At the end of every frame you call clear() on the list to prepare for the next frame. Obviously you would not want the list<> to deallocate and reallocate memory every time you did that. So does clear() release the allocated memory, or does it keep it and reuse it when you add new items? I read in another post here that it keeps the memory. But I know there are different implementations of the stl, so I'm curious as to whether clear() is gauranteed by the standard not to release the allocated memory. Does anyone know?

Share this post


Link to post
Share on other sites
Advertisement
I've always just used the default allocator, so I guess my follow-up questions would be:

1. What is the behavior of the default allocator regarding clear()?

2. Is there a specific allocator you can use that is gauranteed not to release memory on clear()?

Thanks for your reply.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
I've always just used the default allocator, so I guess my follow-up questions would be:

1. What is the behavior of the default allocator regarding clear()?

2. Is there a specific allocator you can use that is gauranteed not to release memory on clear()?

Thanks for your reply.


It is unspecified if the memory will be deallocated or not when you call clear. All clear is guaranteed to do is: erase(begin(), end()) which will call the destructors of the elements being erased, but as to the question of freeing up the actual nodes allocated, that is implementation defined.

Share this post


Link to post
Share on other sites
I'm not sure if you relize this or not but a container of pointers wont destroy/deallocate memory the pointers refer to just the pointers them selfs.

Anyways it sounds like you wont a fixed sized pooled allocation scheme which is very sensible decision for storing pointers/smart pointers in STL containers instead of the default allocator which is not worth it for small objects such as pointers/smart pointers, boost libraries has a couple of STL compliant allocators here

Share this post


Link to post
Share on other sites
I'm pretty sure he's referring only to the memory that a list allocates itself, not the actual Entity objects themselves. Could be wrong though.

If you can use a vector instead of a list, then I'm pretty sure that it is guaranteed to not deallocate the memory when the size decreases. But a pooled memory allocator sounds like it's what you likely want. As snk_kid said, you might wanna check out the allocators that boost has.

Share this post


Link to post
Share on other sites
Yeah, I'm just talking about the vector<> or list<> memory being released, not whether the actual objects will be destroyed. If you're storing pointers, that obviously has to be done elsewhere. I'll probably use smart pointers for this in the final implementation.

Anyway, I'll look into the boost allocators as suggested. Thanks very much :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!