Can I trust a vector to stay put if I don't add to it?

Discussion in 'C++' started by Steven T. Hatton, May 7, 2004.

  1. My understanding of the containers in the Standard Library is that they can
    move out from under pointers and references if they are resized.
    Stroustrup suggests I can reserve the capacity I will need, and thus avoid
    having the vector (or other container) realocated with a different address
    range. I have the sense this is not explicitly specified in the Standard.
    I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
    an oder version draft of the Standard:

    http://www.kuzbass.ru:8086/docs/isocpp/lib-containers.html#lib.vector
    // lib.vector.capacity capacity:
    size_type size() const;
    size_type max_size() const;
    void resize(size_type sz, T c = T());
    size_type capacity() const;
    bool empty() const;
    void reserve(size_type n);

    Is it reasonable for me to assume my vector /will/ stay put if I don't force
    a reallocation by adding to it?
    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
    Steven T. Hatton, May 7, 2004
    #1
    1. Advertising

  2. Steven T. Hatton wrote:
    > ...
    > Is it reasonable for me to assume my vector /will/ stay put if I don't force
    > a reallocation by adding to it?


    Yes. It is specified in the standard.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, May 7, 2004
    #2
    1. Advertising

  3. Steven T. Hatton

    Leor Zolman Guest

    On Thu, 06 May 2004 20:58:29 -0400, "Steven T. Hatton"
    <> wrote:

    >My understanding of the containers in the Standard Library is that they can
    >move out from under pointers and references if they are resized.
    >Stroustrup suggests I can reserve the capacity I will need, and thus avoid
    >having the vector (or other container) realocated with a different address
    >range. I have the sense this is not explicitly specified in the Standard.
    >I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
    >an oder version draft of the Standard:
    >
    >http://www.kuzbass.ru:8086/docs/isocpp/lib-containers.html#lib.vector
    > // lib.vector.capacity capacity:
    > size_type size() const;
    > size_type max_size() const;
    > void resize(size_type sz, T c = T());
    > size_type capacity() const;
    > bool empty() const;
    > void reserve(size_type n);
    >
    >Is it reasonable for me to assume my vector /will/ stay put if I don't force
    >a reallocation by adding to it?


    The word you're interested here is "invalidates". You wish to avoid
    anything that invalidates references, pointers and iterators into the
    vector. If you search the Standard for "invalidates", you'll find
    23.2.4.2/5, which answers your direct question, and also a bit later the
    section on vector::erase() explains what gets invalidated when you use
    that.
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, May 7, 2004
    #3
  4. "Steven T. Hatton" <> wrote in message
    news:...
    >
    > My understanding of the containers in the Standard Library is that they

    can
    > move out from under pointers and references if they are resized.
    > Stroustrup suggests I can reserve the capacity I will need, and thus avoid
    > having the vector (or other container) realocated with a different address
    > range. I have the sense this is not explicitly specified in the Standard.
    > I have not, however, read the entire ISO/IEC 14882:2003. Here's a link to
    > an oder version draft of the Standard:
    >
    > http://www.kuzbass.ru:8086/docs/isocpp/lib-containers.html#lib.vector
    > // lib.vector.capacity capacity:
    > size_type size() const;
    > size_type max_size() const;
    > void resize(size_type sz, T c = T());
    > size_type capacity() const;
    > bool empty() const;
    > void reserve(size_type n);
    >
    > Is it reasonable for me to assume my vector /will/ stay put if I don't

    force
    > a reallocation by adding to it?



    Yes. However you can get the begin or end (one past the end element) of the
    vector whenever you want, so you can do for example:


    #include <vector>


    int main()
    {
    using std::vector;

    vector<int>a(10);

    vector<int>::iterator p=a.begin()+3;

    *p=7;

    a.push_back(4);

    // p continues to point at a[3]
    p=a.begin()+3;
    }






    Regards,

    Ioannis Vranos
    Ioannis Vranos, May 7, 2004
    #4
    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. John Black
    Replies:
    4
    Views:
    424
    Jeff Flinn
    May 28, 2004
  2. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  3. Replies:
    1
    Views:
    964
  4. Linda
    Replies:
    1
    Views:
    538
    Dominick Baier
    Aug 31, 2006
  5. Guy
    Replies:
    4
    Views:
    176
    David Dorward
    Dec 13, 2003
Loading...

Share This Page