Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector reallocation

Reply
Thread Tools

std::vector reallocation

 
 
Lieven
Guest
Posts: n/a
 
      12-02-2004
Consider:

std::vector<int> vec(100);

vector<int>::iterator it(vec.begin());

for(int i(0); i < 10000000; i++){
vec.push_back(rand());
}

It is possible that vec get's reallocated. My question is: to what will it
point? Does it get reallocated to?


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-02-2004
Lieven wrote:
> Consider:
>
> std::vector<int> vec(100);
>
> vector<int>::iterator it(vec.begin());
>
> for(int i(0); i < 10000000; i++){
> vec.push_back(rand());
> }
>
> It is possible that vec get's reallocated.


It's actually not just possible, it's very likely.

> My question is: to what will it
> point? Does it get reallocated to?


No. If the vector is reallocated 'it' will become invalid.

V
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-02-2004

"Lieven" <> wrote in message
news:41af51e4$0$13470$...
> Consider:
>
> std::vector<int> vec(100);
>
> vector<int>::iterator it(vec.begin());
>
> for(int i(0); i < 10000000; i++){
> vec.push_back(rand());
> }
>
> It is possible that vec get's reallocated.


Yes, possible, and probable. Also note that your
code above results in a vector containing
10000100 elements, the first hundred of which
have a value of zero.

> My question is: to what will it
> point?


A vector does not 'point'. A vector is not a pointer.
It's a container which stores objects.

> Does it get reallocated to?


When a vector's size increases, if necessary, additional
memory will be automatically allocated to accomodate the
new elements. You can 'preallocate' space for your elements
by using 'vector.resize()' or 'vector.reserve()' if desired.

-Mike


 
Reply With Quote
 
chris
Guest
Posts: n/a
 
      12-02-2004
Lieven wrote:
> Consider:
>
> std::vector<int> vec(100);
>
> vector<int>::iterator it(vec.begin());
>
> for(int i(0); i < 10000000; i++){
> vec.push_back(rand());
> }
>
> It is possible that vec get's reallocated. My question is: to what will it
> point? Does it get reallocated to?
>
>


In almost all implications (but of course not guaranted by the standard)
it will continue pointing to whatever bit of memory the original vector
was allocated to, but now that memory won't be owned by the vector any more.

Offically, reading or writing to it is undefined. Unoffically, you are
likely to find that you think you can get away with reading from it for
some time after reallocation, as most systems won't bother wiping the
memory the vector was in until something else is put there. However of
course DO NOT DO IT! Because it's likely to break in all kinds of
interesting ways and be a very difficult bug to track down.

Chris
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-02-2004
Mike Wahler wrote:
> "Lieven" <> wrote in message
> news:41af51e4$0$13470$...
>
>>Consider:
>>
>>std::vector<int> vec(100);
>>
>>vector<int>::iterator it(vec.begin());
>>
>>for(int i(0); i < 10000000; i++){
>> vec.push_back(rand());
>>}
>>
>>It is possible that vec get's reallocated.

>
>
> Yes, possible, and probable. Also note that your
> code above results in a vector containing
> 10000100 elements, the first hundred of which
> have a value of zero.
>
>
>>My question is: to what will it
>>point?

>
>
> A vector does not 'point'. A vector is not a pointer.
> It's a container which stores objects.


'it' here is a variable, the iterator, initialised with 'vec.begin()'.

> [...]


V
 
Reply With Quote
 
Lieven
Guest
Posts: n/a
 
      12-02-2004
Mike Wahler wrote:

>
>> My question is: to what will it
>> point?

>
> A vector does not 'point'. A vector is not a pointer.
> It's a container which stores objects.
>

This is of course my fault for using bad names and typos, but the 'it'
refers to the iterator named 'it'.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-02-2004

"Victor Bazarov" <> wrote in message
news:BDIrd.11643$. verio.net...
> Mike Wahler wrote:
> > "Lieven" <> wrote in message
> > news:41af51e4$0$13470$...
> >
> >>Consider:
> >>
> >>std::vector<int> vec(100);
> >>
> >>vector<int>::iterator it(vec.begin());
> >>
> >>for(int i(0); i < 10000000; i++){
> >> vec.push_back(rand());
> >>}
> >>
> >>It is possible that vec get's reallocated.

> >
> >
> > Yes, possible, and probable. Also note that your
> > code above results in a vector containing
> > 10000100 elements, the first hundred of which
> > have a value of zero.
> >
> >
> >>My question is: to what will it
> >>point?

> >
> >
> > A vector does not 'point'. A vector is not a pointer.
> > It's a container which stores objects.

>
> 'it' here is a variable, the iterator, initialised with 'vec.begin()'.


Ah, right, I see that now. I thought he was using the English word
'it' (referring to the vector).

-Mike


 
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
Re: Memory reallocation CBFalconer C Programming 2 03-12-2009 11:53 PM
reallocation George2 C++ 0 03-05-2008 03:17 AM
Vector reallocation newbiecpp C++ 9 07-27-2004 04:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57