Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #41253 > unrolled thread

do shared_ptrs to resized vector remain valid?

Started byjomarbueyes@hotmail.com
First post2016-02-26 07:41 -0800
Last post2016-02-26 18:08 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  do shared_ptrs to resized vector remain valid? jomarbueyes@hotmail.com - 2016-02-26 07:41 -0800
    Re: do shared_ptrs to resized vector remain valid? Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 18:18 +0200
      Re: do shared_ptrs to resized vector remain valid? legalize+jeeves@mail.xmission.com (Richard) - 2016-02-26 16:56 +0000
        Re: do shared_ptrs to resized vector remain valid? jomarbueyes@hotmail.com - 2016-02-26 09:05 -0800
      Re: do shared_ptrs to resized vector remain valid? jomarbueyes@hotmail.com - 2016-02-26 09:00 -0800
    Re: do shared_ptrs to resized vector remain valid? Juha Nieminen <nospam@thanks.invalid> - 2016-02-26 18:08 +0000

#41253 — do shared_ptrs to resized vector remain valid?

Fromjomarbueyes@hotmail.com
Date2016-02-26 07:41 -0800
Subjectdo shared_ptrs to resized vector remain valid?
Message-ID<5735d6ff-0572-41ff-ac88-8b1bcd52cef6@googlegroups.com>
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

[toc] | [next] | [standalone]


#41256

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-26 18:18 +0200
Message-ID<fOOdnXanTNJK503LnZ2dnUU78W-dnZ2d@giganews.com>
In reply to#41253
On 26.02.2016 17:41, jomarbueyes@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

[toc] | [prev] | [next] | [standalone]


#41257

Fromlegalize+jeeves@mail.xmission.com (Richard)
Date2016-02-26 16:56 +0000
Message-ID<naq040$f0$1@news.xmission.com>
In reply to#41256
[Please do not mail me a copy of your followup]

Paavo Helde <myfirstname@osa.pri.ee> spake the secret code
<fOOdnXanTNJK503LnZ2dnUU78W-dnZ2d@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>

[toc] | [prev] | [next] | [standalone]


#41259

Fromjomarbueyes@hotmail.com
Date2016-02-26 09:05 -0800
Message-ID<ab5eef44-442f-4208-8fc2-eaa450c9db0c@googlegroups.com>
In reply to#41257
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

[toc] | [prev] | [next] | [standalone]


#41258

Fromjomarbueyes@hotmail.com
Date2016-02-26 09:00 -0800
Message-ID<ec35c568-9509-4e07-b7ac-2e93cfb25b44@googlegroups.com>
In reply to#41256
On Friday, February 26, 2016 at 11:18:42 AM UTC-5, Paavo Helde wrote:
> On 26.02.2016 17:41, jomarREMOVEbueyes at hotREMOVEmail dot 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

Hi Paavo,

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

Thanks again, 

Jomar

[toc] | [prev] | [next] | [standalone]


#41262

FromJuha Nieminen <nospam@thanks.invalid>
Date2016-02-26 18:08 +0000
Message-ID<naq4aq$b4$1@adenine.netfront.net>
In reply to#41253
jomarbueyes@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: news@netfront.net ---

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web