Jump to content
  • Advertisement
Sign in to follow this  
ill

C++ Map Memory Reuse

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

I have a question about C++ std::map.

When you use vector it's a growing array. It reallocates a block of memory for storage if needed. I can remove elements from the back of the list and the size of the vector grows smaller. The memory that is allocated to it stays constant though which is one nice thing about vectors. They are efficient for certain cases because of this. If I need to add more elements back in to the array the memory doesn't need to be reallocated.

Now let's say I have a map of some int to a vector.

std::map<unsigned int, std::vector<unsigned int> >

I put a mapping of 5 to a new std::vector because currently there is no vector in the map at 5 yet.

Now let's say I clear the map. Was the memory allocated for storing the vector freed or is that memory still around and will be reused for a new element in the map?
Let's say I now put a new vector with a key of 4. Will the memory that was allocated for the vector stored at 5 be reused for the vector stored at 4?

I hope this question isn't too confusingly worded.

The reason I'm asking is this is something used by my rendering engine and the std::map stores which static triangle indeces are in the view frustum this frame. It's a mapping of material index to lists of triangle indeces so I can sort the triangles I render by material easily to avoid switching active textures and shaders. This will be updated every frame and inefficiency in this area due to constant memory reallocation could severely hamper the FPS. I might try to use more efficient data structures in the future but for now my engine is in the early stages and I'm using the STL for most things. I am also open to suggestions of more efficient data structures to use in place of a map if anyone has good ideas.

Share this post


Link to post
Share on other sites
Advertisement
When you remove the map element, the value_type will be destructed (in effect, deleting all memory the vector uses). When you add a new element, a new vector will be constructed. So, no, the vector::capacity() will be whatever a new vector allocates when its newly constructed.

Share this post


Link to post
Share on other sites
A suggestion I have is to create a wrapper class for your index buffer.

Something like this

class IndexBuffer
{
vector<unsigned int> mIndices;
public:
};


Then the map could be a mapping of an int to raw pointer or shared_ptr of type IndexBuffer.

Share this post


Link to post
Share on other sites
It will be freed yes, but the allocator that the CRT uses will probably keep it on a free list organised by block size such that another memory allocation request for a similar size block will likely use the same piece of memory, making reallocation not too bad and increasing cache locality of reference. Best not to worry about it yet.

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!