Is the storage for a std::vector<T> guaranteed to be contiguous? (from FAQ)

Discussion in 'C++' started by Newsgroup - Ann, Aug 15, 2003.

  1. Hi:

    I saw the following codes from the FAQ about the contiguous storage of
    vector. I am just wondering how does the implementation of the <vector>
    guarantee the contiguous? What if a vector v was defined first as a
    5-element vector, and then was increased dramatically but the corresponding
    contiguous area has already been occupied by other objects? Maybe sounds
    stupid to you gurus, but it does confuse me:( Thanks.

    ----------------------
    #include <vector>
    #include "Foo.h" /* get class Foo */

    // old-style code that wants an array
    void f(Foo* array, unsigned numFoos);

    void g()
    {
    std::vector<Foo> v;
    ...
    f(&v[0], v.size()); ? safe
    }
    ----------------------------
     
    Newsgroup - Ann, Aug 15, 2003
    #1
    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. Ravi
    Replies:
    8
    Views:
    10,310
    Mike Wahler
    Mar 6, 2004
  2. Olumide
    Replies:
    9
    Views:
    1,919
    Dan Pop
    Nov 30, 2004
  3. Gary Kuehn
    Replies:
    2
    Views:
    463
    Gary Kuehn
    Jul 19, 2005
  4. Dave Rahardja

    Contiguous storage for <vector>

    Dave Rahardja, Oct 25, 2005, in forum: C++
    Replies:
    3
    Views:
    473
    Dave Rahardja
    Oct 26, 2005
  5. Replies:
    3
    Views:
    384
    James Kanze
    Dec 21, 2007
Loading...

Share This Page