Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL: valid pointer to element of a list or vector container?

Reply
Thread Tools

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

 
 
Rui Maciel
Guest
Posts: n/a
 
      05-05-2010
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
 
Reply With Quote
 
 
 
 
cpp4ever
Guest
Posts: n/a
 
      05-05-2010
On 05/05/2010 01:46 AM, Rui Maciel wrote:
> 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


 
Reply With Quote
 
 
 
 
thomas
Guest
Posts: n/a
 
      05-05-2010
On May 5, 9:07*am, cpp4ever <(E-Mail Removed)> wrote:
> On 05/05/2010 01:46 AM, Rui Maciel wrote:
>
> > 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


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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-05-2010
On 5/5/2010 12:40 AM, thomas wrote:
> On May 5, 9:07 am, cpp4ever<(E-Mail Removed)> wrote:
>> On 05/05/2010 01:46 AM, Rui Maciel wrote:
>>
>>> 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

>
> 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.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-11-2010
Francis Glassborow wrote:

> 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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-11-2010
Sam wrote:

> 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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-11-2010
cpp4ever wrote:

> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do vector iterators remain valid after resize() if base pointer ofelements remains unchanged? zr C++ 18 03-28-2009 11:19 AM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
Valid pointer to map element after erase gfiuni2 C++ 7 08-25-2006 12:56 PM
Can list container contain an vector object, such as list< vector<string> >?? ehui928 C++ 2 05-29-2006 01:09 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments