Jump to content
  • Advertisement
Sign in to follow this  
Aface

Style Question 1 - Pointers To Vector Objects

This topic is 4901 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 Guys, I often use vectors to store my game objects - and any other objects that require a pointer to these objects will store the a pointer to them. The problem is that once I have set these pointers, If i need to add to the vector enough, then it will need to grow and my pointers become invalid as the array chooses a newer and bigger chunk of memory. My question is - what is the most elegant way to get around this? I can see a few ways 1. Store a pointer to the array and an index instead of a pointer to the object (seems to me like there should be a better way). 2. Use a non-contiguous container like a list (perhaps the better way to do it, but may reduce speed when iterating through the game objects?) 3. When the game is initialised, .reserve() enough elements in the array so as to prevent there ever being a memory move (seems like a waste if i guess too high, and crashes if i guess to low). Am I missing something? Is it bad form to store pointers in this way? Regards AlexS

Share this post


Link to post
Share on other sites
Advertisement
The method I usually use is to create a vector of pointers instead
I just allocate memory for each item when I push & delete each item before popping it. That way the address of the pointers will change, but they will always point to fixed locations ;]

It's either that or use some other storage container or store indexes instead

Share this post


Link to post
Share on other sites
The problem you have is typically known as object ownership.

The first question i would ask is contiguous of elements important at the level of abstraction? in this context its game objects in which case most likely not important.

It seems to me you want to share instances, therefore a logical conclusion would be to store reference counted smart pointers instead like boost::shared/intrusive_ptr in some appropriate container(s), you can still use custom memory management for the actual instances, such as overloading operators new/delete for class and class hierarchies there you could use pooling schemes. The Loki library provides a custom small object allocator you can easily add & use, and boost has a package of pooled allocators, two of which are STL compliant (boost::pool_allocator/fast_pool_allocator).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!