Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Map Memory Reuse

Started by ill Nov 12, 2011 at 2:40 AM 3 replies 2.1k views
Original Post
ill
ill
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 >

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.
BradBobak_68669
BradBobak_68669
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.
HappyCoder
HappyCoder
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.
My current game project Platform RPG
Bregma
Bregma
(1) profile before you optimize
(2) read up on allocators in the standard library
Stephen M. Webb
Professional Free Software Developer
iMalc
iMalc
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.