Object in list invalidation question

M

matt.elkins

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!
 
J

James Kanze

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?

Yes, for std::list, std::set and std::map. (It isn't guaranteed
for other container types.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top