[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  STL
. . -> [Subject]  Deconstructors and lists

Deconstructors and lists

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Thu 28 Jul 2005 03:58 AM (UTC)
Message
I have a class that has a deconstructor. In the deconstructor function, I remove it from the list that it is in with this:


OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
        std::list<OLC_BOUNTY_DATA * >::iterator iter;
        OLC_BOUNTY_DATA * bounty;

        for (iter = olc_bounties.begin(); iter != olc_bounties.end(); iter++ )
        {
            bounty = (*iter);

            if ( bounty == this)
            {
                olc_bounties.erase(iter);
            }
        }
}


Can someone tell me what I'm doing wrong? It keeps crashing when iter is dereferenced to be assigned into bounty.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by Darrik Vequir   (24 posts)  [Biography] bio
Date Reply #1 on Thu 28 Jul 2005 12:45 PM (UTC)
Message

You want to add a 'return' after olc_bounties.erase call. the iterator behaves in an undefined fashion if you erase something from a list but continue iterating through it.

DV
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #2 on Thu 28 Jul 2005 04:02 PM (UTC)
Message
... How did I miss that? Thanks.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #3 on Mon 01 Aug 2005 07:03 PM (UTC)
Message
Another thing you can do is to iterate over a copy of the list, removing from the original. That's something you'd need to do when you need to remove multiple elements. Of course, there is a whole new set of pitfalls, e.g. the copied list obviously won't have the same iterator pointers as the old list...

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Darrik Vequir   (24 posts)  [Biography] bio
Date Reply #4 on Tue 02 Aug 2005 01:54 PM (UTC)
Message

Haven't hit a situation where I can't just it = list.begin() after an erase... or, I should say, haven't had a situation where the list is big enough to matter if it has to reset and re-search 4-5 times.

I usually try to use other containers if I get into those situations.

DV
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Tue 02 Aug 2005 05:09 PM (UTC)
Message
Well, right, you can always just restart the whole iteration when you erase. But as you imply, that's really not very efficient when you have large lists, and even less efficient when comparing elements is a slow operation. What other containers would you have in mind?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Darrik Vequir   (24 posts)  [Biography] bio
Date Reply #6 on Wed 03 Aug 2005 12:54 PM (UTC)
Message

I've mostly used maps for any large list I may need. I can find SOME key to look it up by that avoids having to iterate through the entire list to find something! :).

then again, I've just barely gotten the database working on my scratch base, have the most rudimentary of object structure, so its possible I may have to come up with a better system in the future.

DV
[Go to top] top

Posted by Kiasyn Kelle   (15 posts)  [Biography] bio
Date Reply #7 on Sun 25 Sep 2005 10:41 AM (UTC)
Message
I think you can use something like:


OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
std::list<OLC_BOUNTY_DATA * >::iterator iter;
OLC_BOUNTY_DATA * bounty;

for (iter = olc_bounties.begin(); iter != olc_bounties.end(); )
{
bounty = (*iter);

if ( bounty == this)
olc_bounties.erase(iter++);
else
++iter;
}
}


[Go to top] top

Posted by Samson   USA  (683 posts)  [Biography] bio
Date Reply #8 on Sun 25 Sep 2005 04:33 PM (UTC)
Message

OLC_BOUNTY_DATA::~OLC_BOUNTY_DATA()
{
        std::list<OLC_BOUNTY_DATA * >::iterator iter;
        OLC_BOUNTY_DATA * bounty;

        for (iter = olc_bounties.begin(); iter != olc_bounties.end(); )
        {
            bounty = (*iter);
            iter++;

            if ( bounty == this)
            {
                olc_bounties.remove(bounty);
            }
        }
}


You can also do it like so. All of my std::list calls use the remove() member instead of erase() and I set them all up in a similar manner to the above. They all work just fine this way for multiple member removal.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


26,620 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]