STL: valid pointer to element of a list or vector container?

R

Rui Maciel

Is there a way to get a valid pointer to an element belonging to a list or vector container that is
capable of remaining valid throughout the "life cycle" of the container data structure?


Thanks in advance,
Rui Maciel
 
C

cpp4ever

Is there a way to get a valid pointer to an element belonging to a list or vector container that is
capable of remaining valid throughout the "life cycle" of the container data structure?


Thanks in advance,
Rui Maciel

IMHO the answer has to be no. Unless you can be sure the memory used by
the container never changes, or the container contents don't change then
I reckon all bets are off. You simply too little knowledge or control of
the internals of most, if not all, container implementations for that to
ever be likely.

JB
 
T

thomas

IMHO the answer has to be no. Unless you can be sure the memory used by
the container never changes, or the container contents don't change then
I reckon all bets are off. You simply too little knowledge or control of
the internals of most, if not all, container implementations for that to
ever be likely.

JB

Sometimes.
The vector may double its size for an insertion, in which case data is
copied to new place and old pointer-like container gets invalidated.
But if expansion doesn't happen, your method works.
So I suggest always using interfaces provided by the container. You
should not make any assumption to the implementation details.
 
D

Daniel Pitts

Sometimes.
The vector may double its size for an insertion, in which case data is
copied to new place and old pointer-like container gets invalidated.
But if expansion doesn't happen, your method works.
So I suggest always using interfaces provided by the container. You
should not make any assumption to the implementation details.
Or if an element is removed or inserted before the item you are holding
a pointer to.
 
R

Rui Maciel

Francis said:
How could there be? Suppose I obtain a pointer to the fifth element and
then reduce the container so that it only has 4 elements. Now what
should happen?

I am assuming that no element is popped from the container. which is why I mentioned that any given
element is capable of remaining valid throughout the "life cycle" of the container data structure.


Rui Maciel
 
R

Rui Maciel

Sam said:
Yes for a list, no for a vector. A list iterator is only invalidated when:

=====

23.2.2.3 list modifiers
[lib.list.modifiers]
<snip/>

Nice. List it is, then. Thanks for the help, Sam. Kudos!


Rui Maciel
 
R

Rui Maciel

cpp4ever said:
IMHO the answer has to be no. Unless you can be sure the memory used by
the container never changes, or the container contents don't change then
I reckon all bets are off. You simply too little knowledge or control of
the internals of most, if not all, container implementations for that to
ever be likely.

I'm assuming that the containers will be a part of a data structure which would only be freed at the
end of it's "life cycle". To put it in other words, the data structured is assembled, accessed and
deleted in that order.


Rui Maciel
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top