473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

stl list's const_itertor problem

Hi,

I'm using stl's list class. For iterating it I'm using its const_iterator.
Lets say I have a:
list<char* >::const_iterator m_myitr;
Now:
void proc1()
{
if (some_condition_true)
mylist.remove ( m_myitr );
}

void proc2()
{
// here we have to use m_myitr assuming that
// its pointing to a valid data in the list which hasnt been removed
char * tmp = * m_myitr;
}

void useall()
{
// lets initialize m_myitr .....
m_myitr = mylist.begin();
proc1();
proc2();

}

The problem is at proc2(). If m_myitr has been removed in proc1() I'm going
to get an exception here which of course I want to avoid. There should be
some *if* check which is what I'm not able to write cuz I dont know how
iterator class is behaving/working.

Please tell me that *if* condition that i should write before assigning its
value to char * tmp;. so that I know that it was removed or not .

Regards,

-ab.
Oct 29 '06 #1
7 1056
I made a mistake writing the proc1()'s remove method sample code, it should
have been passed as " " * m_myitr " not just "m_myitr".

-ab.
"Abubakar" <em**********@yahoo.comwrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
Hi,

I'm using stl's list class. For iterating it I'm using its const_iterator.
Lets say I have a:
list<char* >::const_iterator m_myitr;
Now:
void proc1()
{
if (some_condition_true)
mylist.remove ( m_myitr );
}

void proc2()
{
// here we have to use m_myitr assuming that
// its pointing to a valid data in the list which hasnt been removed
char * tmp = * m_myitr;
}

void useall()
{
// lets initialize m_myitr .....
m_myitr = mylist.begin();
proc1();
proc2();

}

The problem is at proc2(). If m_myitr has been removed in proc1() I'm
going to get an exception here which of course I want to avoid. There
should be some *if* check which is what I'm not able to write cuz I dont
know how iterator class is behaving/working.

Please tell me that *if* condition that i should write before assigning
its value to char * tmp;. so that I know that it was removed or not .

Regards,

-ab.


Oct 29 '06 #2
I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?

regards,

-ab.

"Abubakar" <em**********@yahoo.comwrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
Hi,

I'm using stl's list class. For iterating it I'm using its const_iterator.
Lets say I have a:
list<char* >::const_iterator m_myitr;
Now:
void proc1()
{
if (some_condition_true)
mylist.remove ( m_myitr );
}

void proc2()
{
// here we have to use m_myitr assuming that
// its pointing to a valid data in the list which hasnt been removed
char * tmp = * m_myitr;
}

void useall()
{
// lets initialize m_myitr .....
m_myitr = mylist.begin();
proc1();
proc2();

}

The problem is at proc2(). If m_myitr has been removed in proc1() I'm
going to get an exception here which of course I want to avoid. There
should be some *if* check which is what I'm not able to write cuz I dont
know how iterator class is behaving/working.

Please tell me that *if* condition that i should write before assigning
its value to char * tmp;. so that I know that it was removed or not .

Regards,

-ab.


Oct 29 '06 #3
Hi,

I would propose:

void proc1()
{
if(some_condition)
{
mylist.remove(m_myitr);
m_myitr = mylist.end();
}
}

void proc2()
{
if(m_myitr != mylist.end())
{
char* tmp = *m_myitr;
}
}

If they are call from multiple threads you must protect m_myitr and mylist
from being changed concurrently.

--
SvenC

"Abubakar" <em**********@yahoo.comwrote in message
news:up****************@TK2MSFTNGP03.phx.gbl...
>I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?

regards,

-ab.

"Abubakar" <em**********@yahoo.comwrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
>Hi,

I'm using stl's list class. For iterating it I'm using its
const_iterator. Lets say I have a:
list<char* >::const_iterator m_myitr;
Now:
void proc1()
{
if (some_condition_true)
mylist.remove ( m_myitr );
}

void proc2()
{
// here we have to use m_myitr assuming that
// its pointing to a valid data in the list which hasnt been removed
char * tmp = * m_myitr;
}

void useall()
{
// lets initialize m_myitr .....
m_myitr = mylist.begin();
proc1();
proc2();

}

The problem is at proc2(). If m_myitr has been removed in proc1() I'm
going to get an exception here which of course I want to avoid. There
should be some *if* check which is what I'm not able to write cuz I dont
know how iterator class is behaving/working.

Please tell me that *if* condition that i should write before assigning
its value to char * tmp;. so that I know that it was removed or not .

Regards,

-ab.



Oct 30 '06 #4
Thanks.

-ab.

"SvenC" <Sv***@community.nospamwrote in message
news:uN****************@TK2MSFTNGP04.phx.gbl...
Hi,

I would propose:

void proc1()
{
if(some_condition)
{
mylist.remove(m_myitr);
m_myitr = mylist.end();
}
}

void proc2()
{
if(m_myitr != mylist.end())
{
char* tmp = *m_myitr;
}
}

If they are call from multiple threads you must protect m_myitr and mylist
from being changed concurrently.

--
SvenC

"Abubakar" <em**********@yahoo.comwrote in message
news:up****************@TK2MSFTNGP03.phx.gbl...
>>I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?

regards,

-ab.

"Abubakar" <em**********@yahoo.comwrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
>>Hi,

I'm using stl's list class. For iterating it I'm using its
const_iterator. Lets say I have a:
list<char* >::const_iterator m_myitr;
Now:
void proc1()
{
if (some_condition_true)
mylist.remove ( m_myitr );
}

void proc2()
{
// here we have to use m_myitr assuming that
// its pointing to a valid data in the list which hasnt been removed
char * tmp = * m_myitr;
}

void useall()
{
// lets initialize m_myitr .....
m_myitr = mylist.begin();
proc1();
proc2();

}

The problem is at proc2(). If m_myitr has been removed in proc1() I'm
going to get an exception here which of course I want to avoid. There
should be some *if* check which is what I'm not able to write cuz I dont
know how iterator class is behaving/working.

Please tell me that *if* condition that i should write before assigning
its value to char * tmp;. so that I know that it was removed or not .

Regards,

-ab.




Oct 30 '06 #5

Abubakar a écrit :
I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?
NO! You are relying on the internal implementation details of the list
iterator (everythng that begins with "_" + Uppercase or "__" is an
implementation detail of the CRT).

It will not work on another computer, and it may not work on next
version of VC.

The rule is that a list iterator is invalidated when the element it
points to is removed from the list (baware that this is the rule for
list : rules for other containers are different).

Using an invalid iterator is Undefined Behaviour (UB), so there is no
way to detect wether an iterator is valid or not - think of it as
dereferencing a pointer you've just called delete upon.
Therefore, you need to track in your algorithms when an iterator is
valid or not. You've already been given an example.

Arnaud
MVP - VC

Oct 30 '06 #6
Abubakar wrote:
I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?
Well, it's correct in that I think it works on one specific
implementation with one specific set of compiler/library options. It's
incorrect in that it won't work anywhere else, and is non-standard.
Avoid using anything starting with a _ and a capital letter or
containing a double underscore, unless you have read any documentation
relating to it (there usually won't be any) and accept the extreme
limitations (usually that your code will not port to any other compiler,
including often the next version of the one you are using).

Setting an iterator to cont.end() and checking for that is the most
acceptable way of creating a "null" iterator. If you don't have a
specific container to use that you can guarantee will stay live as long
as m_myitr will, you'll need a second variable, bool m_ismyitrOk or similar.

Tom
Oct 30 '06 #7
Thanks guys. Arnaud & Tom.

regards,

-ab.

<ad******@club-internet.frwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...

Abubakar a écrit :
I have tried and the following is working:

if ( m_myitr._Mycont == NULL )
this means its been removed;
else
not removed.

I wanna know is this the correct way?
NO! You are relying on the internal implementation details of the list
iterator (everythng that begins with "_" + Uppercase or "__" is an
implementation detail of the CRT).

It will not work on another computer, and it may not work on next
version of VC.

The rule is that a list iterator is invalidated when the element it
points to is removed from the list (baware that this is the rule for
list : rules for other containers are different).

Using an invalid iterator is Undefined Behaviour (UB), so there is no
way to detect wether an iterator is valid or not - think of it as
dereferencing a pointer you've just called delete upon.
Therefore, you need to track in your algorithms when an iterator is
valid or not. You've already been given an example.

Arnaud
MVP - VC
Oct 31 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kin®sole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.