array of vector!

Discussion in 'C++' started by TF, Jul 23, 2003.

  1. TF

    TF Guest

    I have a fixed array of vectors like:

    vector<string> a_vStr[10];

    Then I grow each vector item and use it later. Everything works fine
    but I am curious about following potential problem.

    When we grow a vector it may copy all of its elements to different
    memory location to accomodate the new vector size. Is it possible that
    at some point an element of fixed array e.g. a_vStr[5] gets invalid or
    does not point to the vector it was pointing to at begining?
    TF, Jul 23, 2003
    #1
    1. Advertising

  2. "TF" <> wrote in message
    news:...
    > I have a fixed array of vectors like:
    >
    > vector<string> a_vStr[10];
    >
    > Then I grow each vector item and use it later. Everything works fine
    > but I am curious about following potential problem.
    >
    > When we grow a vector it may copy all of its elements to different
    > memory location to accomodate the new vector size. Is it possible that
    > at some point an element of fixed array e.g. a_vStr[5] gets invalid or
    > does not point to the vector it was pointing to at begining?


    No, because the size of the vector object itself remains unchanged
    during run-time. The size of the vector object itself is independant of
    the number of elements it holds during run-time. Like any other class,
    the size of the vector object is determined during compile time and
    therefore is fixed. Modifying one vector in the a_vStr[] array will not
    affect the other vectors in that array

    For vector objects there are two blocks of memory needed; one to store
    the vector object itself (in your example that would be a element in the
    a_vStr[] array), and another to hold the elements of the vector (those
    will be stored outside the a_vStr[] array). Certain operations on a
    vector may cause the elements stored in the vector to be moved in
    memory, however the memory location of the vector object itself will not
    change. In other words iterators or pointers to elements in a vector may
    become invalid if the vector changes, but the vectors in the a_vStr
    array will remain valid.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl.
    Peter van Merkerk, Jul 23, 2003
    #2
    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. pmatos
    Replies:
    6
    Views:
    23,790
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    317
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    359
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page