• Advertisement
Sign in to follow this  

Converting a list iterator to a const pointer

This topic is 3003 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
You don't need the extra brackets. It's common to write just &*iter

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
Good point. I knew it seemed weird to have only one item in the first list.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement