Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

do shared_ptrs to resized vector remain valid?

32 views
Skip to first unread message

jomar...@hotmail.com

unread,
Feb 26, 2016, 10:42:35 AM2/26/16
to
Hi,

I'm using a few shared pointers to a large vector that might be resized (pointers to the vector itself, not to individual elements). My question is: Do all the shared pointers still point to the vector after it is resized? Thus far, it seems that they do with g++ version 5.2.0. However, for portability I'd like to know what the standard says.

Thank you in advance for any help,

Jomar

Paavo Helde

unread,
Feb 26, 2016, 11:18:42 AM2/26/16
to
On 26.02.2016 17:41, jomar...@hotmail.com wrote:
> Hi,
>
> I'm using a few shared pointers to a large vector that might be resized (pointers to the vector itself, not to individual elements). My question is: Do all the shared pointers still point to the vector after it is resized? Thus far, it seems that they do with g++ version 5.2.0. However, for portability I'd like to know what the standard says.

std::vector consists of the std::vector object itself and of a separate
resizable buffer containing the elements. Once created, the std::vector
object stays at the same location in memory (an object in C++ *is* an
area of memory) until it is destroyed, so any smart or non-smart
pointers to it remain valid during its lifetime.

With the elements in the resizable buffer the things are different,
their location can change during operations like resizing, so the
pointers and references to the elements can become invalid - but you
already know that.

hth
Paavo

Richard

unread,
Feb 26, 2016, 11:56:50 AM2/26/16
to
[Please do not mail me a copy of your followup]

Paavo Helde <myfir...@osa.pri.ee> spake the secret code
<fOOdnXanTNJK503L...@giganews.com> thusly:

>With the elements in the resizable buffer the things are different,
>their location can change during operations like resizing, [...]

I believe the standard guarantees that they remain in the same
location if you resize the vector smaller.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

jomar...@hotmail.com

unread,
Feb 26, 2016, 12:00:56 PM2/26/16
to
On Friday, February 26, 2016 at 11:18:42 AM UTC-5, Paavo Helde wrote:
Hi Paavo,

Thank you very much for your response. It really helps.

Thanks again,

Jomar

jomar...@hotmail.com

unread,
Feb 26, 2016, 12:05:26 PM2/26/16
to
On Friday, February 26, 2016 at 11:56:50 AM UTC-5, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Paavo Helde <myfirstname at osa.pri.ee> spake the secret code
> <fOOdnXanTNJK503LnZ2dnUU78W-dnZ2d at giganews.com> thusly:
>
> >With the elements in the resizable buffer the things are different,
> >their location can change during operations like resizing, [...]
>
> I believe the standard guarantees that they remain in the same
> location if you resize the vector smaller.
> --
> "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
> The Computer Graphics Museum <http://computergraphicsmuseum.org>
> The Terminals Wiki <http://terminals.classiccmp.org>
> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Hi Richard,

Thank you very much for your response. My concern is mostly with growing a vector.

Thank you again,

Jomar

Juha Nieminen

unread,
Feb 26, 2016, 1:08:44 PM2/26/16
to
jomar...@hotmail.com wrote:
> I'm using a few shared pointers to a large vector that might be
> resized (pointers to the vector itself, not to individual
> elements). My question is: Do all the shared pointers still point to
> the vector after it is resized? Thus far, it seems that they do with
> g++ version 5.2.0. However, for portability I'd like to know what
> the standard says.

Do you mean a vector of shared_ptr's, or a vector of objects (and
then those separate shared_ptr's pointing to said objects)?

In the latter case all pointers and iterators get invalidated if
the vector is resized larger. (Although I think that if you reserved
a larger size and then resize the vector to be at most that size,
they probably don't get invalidated.)

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages