Thread View: comp.lang.c++.moderated
5 messages
5 total messages
Started by Bowden Wise
Fri, 29 Dec 1995 21:40
STL loops through a list with one element more than once! Why?
Author: Bowden Wise
Date: Fri, 29 Dec 1995 21:40
Date: Fri, 29 Dec 1995 21:40
48 lines
1951 bytes
1951 bytes
X-Original-Date: Fri, 29 Dec 1995 16:21:37 -0800
Hi,
I am using the STL to hold a list of items for a DDEML topic. When
the topic is destroyed, all of the associated items with that
topic are erased from the topic's item list. Erasing the item
also causes the item's destructor to be called:
BOOL DDETopic::DelAllItems ()
{
#ifdef DEBUGON
Tracer t("DDETopic::DelAllItems ()");
#endif
for (_itemIter = _itemList.begin();
_itemIter != _itemList.end();
_itemIter++)
{
#ifdef DEBUGON
if (__theLog) __theLog->OutputLn ("calling erase for item");
#endif
_itemList.erase (_itemIter);
}
return TRUE;
}
In my particular application the list only contains a single
element.
When DEBUGON is enabled, the single item is correctly deleted the
first time through the loop. However, I can also verify that
the loop is entered a second time, thereby resulting in a GPF.
Any ideas why the _itemIter != _itemList.end() test of the for
loop is failing? Anyone else encountered this?
Bowden
Dept of Computer Science, RPI
wiseb@cs.rpi.edu
http://www.cs.rpi.edu/~wiseb/
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Re: STL loops through a list with one element more than once! Why?
Author: Yossef Halakhmi
Date: Mon, 01 Jan 1996 17:47
Date: Mon, 01 Jan 1996 17:47
28 lines
1541 bytes
1541 bytes
X-Original-Date: 31 Dec 1995 07:54:48 GMT
In article <4c1n8o$2jn@netlab.cs.rpi.edu>, Bowden Wise <wiseb@cs.rpi.edu> wrote:
> BOOL DDETopic::DelAllItems ()
> {
> for (_itemIter = _itemList.begin();
> _itemIter != _itemList.end();
> _itemIter++)
> {
> _itemList.erase (_itemIter);
> }
> return TRUE;
> }
After any one 'erase' your list is changed and iterator doesn't point to
the objrct that you want . _itemList.end() also will point on something new.
And not to the old value of _itemList.end() which you have in
'for' .
If you want to erase all elements in list/array then use
erase(iterator forst, iterator last) mem-function
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Re: STL loops through a list with one element more than once! Why?
Author: Terris Linenbach
Date: Mon, 01 Jan 1996 17:48
Date: Mon, 01 Jan 1996 17:48
68 lines
2635 bytes
2635 bytes
X-Original-Date: 1 Jan 1996 05:46:03 GMT
"erase" invalidates all active iterators on the container. The results are
undefined.
Do this instead:
_itemList.erase( _itemList.begin(), _itemList.end() );
- Terris
In article <4c1n8o$2jn@netlab.cs.rpi.edu>, wiseb@cs.rpi.edu says...
>
>X-Original-Date: Fri, 29 Dec 1995 16:21:37 -0800
>
>Hi,
>
>I am using the STL to hold a list of items for a DDEML topic. When
>the topic is destroyed, all of the associated items with that
>topic are erased from the topic's item list. Erasing the item
>also causes the item's destructor to be called:
>
>
>BOOL DDETopic::DelAllItems ()
>{
>#ifdef DEBUGON
>Tracer t("DDETopic::DelAllItems ()");
>#endif
>
> for (_itemIter = _itemList.begin();
> _itemIter != _itemList.end();
> _itemIter++)
> {
>#ifdef DEBUGON
>if (__theLog) __theLog->OutputLn ("calling erase for item");
>#endif
> _itemList.erase (_itemIter);
> }
>
> return TRUE;
>}
>
>In my particular application the list only contains a single
>element.
>
>When DEBUGON is enabled, the single item is correctly deleted the
>first time through the loop. However, I can also verify that
>the loop is entered a second time, thereby resulting in a GPF.
>
>Any ideas why the _itemIter != _itemList.end() test of the for
>loop is failing? Anyone else encountered this?
>
>Bowden
>Dept of Computer Science, RPI
>wiseb@cs.rpi.edu
>http://www.cs.rpi.edu/~wiseb/
>
> [ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
> [ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
> [ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
> [ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Re: STL loops through a list with one element more than once! Why?
Author: Doug Morgan
Date: Tue, 02 Jan 1996 20:31
Date: Tue, 02 Jan 1996 20:31
30 lines
1654 bytes
1654 bytes
X-Original-Date: 02 Jan 1996 18:54:44 GMT
In article <4c1n8o$2jn@netlab.cs.rpi.edu> Bowden Wise <wiseb@cs.rpi.edu> writes:
>...
> for (_itemIter = _itemList.begin();
> _itemIter != _itemList.end();
> _itemIter++)
> {
>...
> _itemList.erase (_itemIter);
> }
> ...
> Any ideas why the _itemIter != _itemList.end() test of the for
> loop is failing? Anyone else encountered this?
The erase() invalidates _itemIter. Continuing the loop is therefore
invalid. Use _itemList.erase(_itemList.begin(), _itemList.end())
instead.
Doug
----------
Doug Morgan TEL: (415) 960-7444 Booz-Allen & Hamilton, Inc.
doug@ads.com (UNIX) FAX: (415) 960-7500 1500 Plymouth St.
morgan_doug@bah.com (MS-Mail) Mountain View, CA 94043-1230
----------
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Re: STL loops through a list with one element more than once! Why?
Author: Alexander Stepanov
Date: Wed, 03 Jan 1996 09:26
Date: Wed, 03 Jan 1996 09:26
50 lines
1895 bytes
1895 bytes
X-Original-Date: 2 Jan 1996 19:18:24 GMT
terris@rahul.net (Terris Linenbach) wrote:
>X-Original-Date: 1 Jan 1996 05:46:03 GMT
>
>"erase" invalidates all active iterators on the container.
I think it is not quite true. Erase invalidates some active iterators on the
container. In case of list, erase invalidates only those iterators that point
to the erased elements.
In this particular case:
for (_itemIter = _itemList.begin();
_itemIter != _itemList.end();
_itemIter++) // <- itemIter is invalid now
{
_itemList.erase (_itemIter);
}
But there is a simple fix:
for (_itemIter = _itemList.begin();
_itemIter != _itemList.end(); )
{
_itemList.erase (_itemIter++);
}
and, of course,
_itemList.erase(_itemList.begin(), _itemList.end());
will do the same thing.
Alex Stepanov
stepanov@mti.sgi.com
Silicon Graphics
2011 N. Shorline Boulevard
Mountain View, CA 94043-1389
tel.: (415)933-6121
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads