Jump to content
  • Advertisement
Sign in to follow this  
andrew1b

Converting a list iterator to a const pointer

This topic is 3293 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 list of a light struct, like this:
list<LIGHT> m_Lights;
and I also have an iterator to pass through every light:
list<LIGHT::iterator iter;
But I have a function that needs a const LIGHT *, like this:
bool AddLight(const LIGHT *pLight)
As I only have each LIGHT as an iterator, I'm passing them through like that:
AddLight(&(*iter));
It works fine. But is it the best way? Is it slow and/or dangerous?

Share this post


Link to post
Share on other sites
Advertisement
The need to keep a list of iterators is a bit odd, can you explain that a little?

Other than that, the main danger is if lights are removed from the container. This will invalidate an iterator pointing at that light, and obviously any pointers to it.

How are you going to keep the two lists in sync, to prevent this?
If you store the pointer in AddLight(), how will you keep it in sync?

Share this post


Link to post
Share on other sites
Quote:
&(*iter)

This is a common idiom. It pays to remember which of the standard containers may move elements around in memory: vector may; list, map/set, and deque do not.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
The need to keep a list of iterators is a bit odd, can you explain that a little?

Other than that, the main danger is if lights are removed from the container. This will invalidate an iterator pointing at that light, and obviously any pointers to it.

How are you going to keep the two lists in sync, to prevent this?
If you store the pointer in AddLight(), how will you keep it in sync?


It's not a list of iterators, it's a common list of LIGHT (list::<light> m_List).
The problem is, the LIGHT struct is about 300 bytes long, so it could be slow to copy the whole struct into the function just to read it. The function will take the values from the pointer, use them and then discard them.

Quote:
Original post by Sneftel
This is a common idiom. It pays to remember which of the standard containers may move elements around in memory: vector may; list, map/set, and deque do not.


Isn't it slow? Does *iter copies the iterator content into a temporary buffer before passing it's address?

Share this post


Link to post
Share on other sites
Sorry, I misread your second declaration of the iterator as a list of iterators. [embarrass]

Quote:

Isn't it slow? Does *iter copies the iterator content into a temporary buffer before passing it's address?

*iter yields a reference, not a copy. Otherwise it would be illegal, you can't take the address of a temporary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
&(*iter)

This is a common idiom. It pays to remember which of the standard containers may move elements around in memory: vector may; list, map/set, and deque do not.

A deque may if you insert/erase elements in the middle.

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!