question about and pointer to its items



 DEVELOP > c-Plus-Plus > question about and pointer to its items

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "lallous"
Date: 13 Oct 2004 07:11:31 AM
Object: question about and pointer to its items
Hello
Given this:
list<mystruct_t> lst;
lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);
Now say I have this:
mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the list.
If I delete "item2" (or second item) will the other pointers still be valid?
--
Elias
.

User: "John Harrison"

Title: Re: question about and pointer to its items 13 Oct 2004 07:25:20 AM
"lallous" <lallous@lgwm.org> wrote in message
news:2t4kfoF1qsp5tU1@uni-berlin.de...

Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the list.

If I delete "item2" (or second item) will the other pointers still be

valid?


If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know that
the standard guarantees that pointers will be OK, but its very likely to
work.
John
.
User: "Sharad Kala"

Title: Re: question about and pointer to its items 13 Oct 2004 07:44:14 AM
"John Harrison" <john_andronicus@hotmail.com> wrote in message

If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid.

I think Standard only talks of iterators and references remaining valid, and
am too lazy to check the Standard :-)
Sharad
.

User: "lallous"

Title: Re: question about and pointer to its items 13 Oct 2004 07:39:06 AM
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2t4l39F1s60o6U1@uni-berlin.de...


"lallous" <lallous@lgwm.org> wrote in message
news:2t4kfoF1qsp5tU1@uni-berlin.de...

Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the
list.

If I delete "item2" (or second item) will the other pointers still be

valid?



If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know
that
the standard guarantees that pointers will be OK, but its very likely to
work.

You mean I change p1, p2, p3 to:
list<mystruct_t>::iterator p1, p2, p3;
then have p1, p2, p3 point to each element, then when any element is deleted
the other iterators are still valid?
--
Elias
.
User: "John Harrison"

Title: Re: question about and pointer to its items 13 Oct 2004 07:50:46 AM
"lallous" <lallous@lgwm.org> wrote in message
news:2t4m3eF1qr7l2U1@uni-berlin.de...

"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2t4l39F1s60o6U1@uni-berlin.de...


"lallous" <lallous@lgwm.org> wrote in message
news:2t4kfoF1qsp5tU1@uni-berlin.de...

Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the
list.

If I delete "item2" (or second item) will the other pointers still be

valid?



If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know
that
the standard guarantees that pointers will be OK, but its very likely to
work.


You mean I change p1, p2, p3 to:
list<mystruct_t>::iterator p1, p2, p3;

then have p1, p2, p3 point to each element, then when any element is

deleted

the other iterators are still valid?

Yes.
john
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER