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

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top