STL, memory allocation, and speed.

Started by
4 comments, last by blizzard999 18 years, 8 months ago
I've just spent all night getting rid of as much of my shoddy code in my engine, and decided I may aswell use STL, i'm new to making game engines, so whats the harm? Anyways, I know vaguely how the std::vector works, but i'm curious on one thing - is it possible to force it to allocate a large chunk of memory for itself so it doesn't need to keep reallocating everytime an object is removed or it is cleared? I am wondering as I store pointers to all meshes to be rendered in a vector, then render them all out at once, but this requires the list to be cleared at the end of each run.
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Advertisement
A vector does not automatically shrink its reserved space when elements are removed. Much like Richard Marx, it holds on to the memory.
Ah thank god for that, is there a way I could allocate all the memory before hand? Or would I just need to shove a load of elements into the vector and then clear it?
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Yes. Use the reserve() function.
Spot on [smile] Might beable to get a little more performance out of my game engine yet!
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
If you really think allocation is the bottleneck you can use different tricks.

Use 'static' vector () , list with garbage collector (list::splice) and in general a custom allocator (implemented by your mem manager)

This topic is closed to new replies.

Advertisement