Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ianlo

STL C++ list

This topic is 5276 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, I was wondering which is a better data structure for a std::list. To have a list of pointers to objects or a list of objects themselves? Would love to hear any comments!

Share this post


Link to post
Share on other sites
Advertisement
I always use a list of pointers. I think the reason is that when you look through the list to find a particular item, it takes more time if the list consists of large data-structures. So when you''re dealing with data that is larger than a pointer, you should use pointers for the list.

Can someone confirm if I''m right or wrong about this?

Share this post


Link to post
Share on other sites
Thanks for the reply.
I was wondering if an additional level of reference would cause a performance slow down. You would have to reference the pointer to get the object and then reference the data in the object.

Wouldn''t it be faster to simply reference the object as you access the list and then reference the data?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It depends of what you do want, there is no real answer to this question, but using pointers is often a better option.
adding a object to the list create a copy of the object, the list get bigger, and doesn''t fit in the cache, etc...

Share this post


Link to post
Share on other sites
No, pointers remain pointers.
A lot of things wouldn''t be possible if they didn''t. Things I do daily...

Hellsp4wn
-----------

I''m having deja-vu and amnesia at the same time. It''s like I think I''ve forgotten this before.

Share this post


Link to post
Share on other sites
I''d also use a list of pointers. It''s vital for polymorphism. You can have a list of pointers to instances of different child-classes derived from the same base class: veeeery useful!
But for simple things you can use the non-pointer style.
(btw, please don''t start 2 topics, I''m repeating myself now)


SwSh website!

Share this post


Link to post
Share on other sites
Just remember to delete all the memory when you''re done with the list! Just calling list.clear() isn''t enough. I learnt this first after using STL lists for years... must''ve leaked gigabytes of precious memory

Share this post


Link to post
Share on other sites
quote:

I was wondering if an additional level of reference would cause a performance slow down. You would have to reference the pointer to get the object and then reference the data in the object.



The vast majority of the time, pointers will be faster. If you add an object to the list, you''ll create a new object. You''ll allocate memory, run the constructor (note: if you don''t have a functional copy constructor, adding an object to a list won''t work at all). And so on. If you add a pointer, you just create a copy of the pointer and add that to the list. So the tiny amount of overhead necessary to dereference a pointer is usually dwarfed by the overhead of instantiating a new object.

Share this post


Link to post
Share on other sites

  • 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!