| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
25 Jan 2008 12:14:23 PM |
| Object: |
Object in list invalidation question |
Hello,
Consider the following situation:
std::list <int> numbers;
numbers.push_back(5);
int * ptr = &(*(numbers.begin()));
// Work on the list
numbers.push_back(7);
numbers.push_back(12);
// etc.
My question is: will ptr always point to valid memory provided the
only operations performed on the list are to add to it (and ptr isn't
reassigned, etc)? Given the obvious implementation of a linked list,
then yes, but is it actually guaranteed (implicitly or otherwise) by
the standard?
Thanks!
.
|
|
| User: "James Kanze" |
|
| Title: Re: Object in list invalidation question |
25 Jan 2008 01:32:40 PM |
|
|
On Jan 25, 7:14 pm, wrote:
Consider the following situation:
std::list <int> numbers;
numbers.push_back(5);
int * ptr =3D &(*(numbers.begin()));
// Work on the list
numbers.push_back(7);
numbers.push_back(12);
// etc.
My question is: will ptr always point to valid memory provided the
only operations performed on the list are to add to it (and ptr isn't
reassigned, etc)? Given the obvious implementation of a linked list,
then yes, but is it actually guaranteed (implicitly or otherwise) by
the standard?
Yes, for std::list, std::set and std::map. (It isn't guaranteed
for other container types.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|

|
Related Articles |
|
|