std::vector reallocation

Discussion in 'C++' started by Lieven, Dec 2, 2004.

  1. Lieven

    Lieven Guest

    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?
    Lieven, Dec 2, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 2, 2004
    #2
    1. Advertising

  3. Lieven

    Mike Wahler Guest

    "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
    Mike Wahler, Dec 2, 2004
    #3
  4. Lieven

    chris Guest

    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
    chris, Dec 2, 2004
    #4
  5. 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
    Victor Bazarov, Dec 2, 2004
    #5
  6. Lieven

    Lieven Guest

    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'.
    Lieven, Dec 2, 2004
    #6
  7. Lieven

    Mike Wahler Guest

    "Victor Bazarov" <> wrote in message
    news:BDIrd.11643$09.us.to.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
    Mike Wahler, Dec 2, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. newbiecpp

    Vector reallocation

    newbiecpp, Jul 26, 2004, in forum: C++
    Replies:
    9
    Views:
    529
    John Harrison
    Jul 27, 2004
  2. Anonymous
    Replies:
    20
    Views:
    4,253
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    703
    Andrew Koenig
    Jan 15, 2006
  4. George2

    reallocation

    George2, Mar 5, 2008, in forum: C Programming
    Replies:
    0
    Views:
    277
    George2
    Mar 5, 2008
  5. Rune Allnor
    Replies:
    4
    Views:
    916
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page