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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven T. Hatton

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

    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:


    // lib.vector.capa city capacity:
    size_type size() const;
    size_type max_size() const;
    void resize(size_typ e sz, T c = T());
    size_type capacity() const;
    bool empty() const;
    void reserve(size_ty pe 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
  • Andrey Tarasevich

    #2
    Re: Can I trust a vector to stay put if I don't add to it?

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

    Yes. It is specified in the standard.

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Leor Zolman

      #3
      Re: Can I trust a vector to stay put if I don't add to it?

      On Thu, 06 May 2004 20:58:29 -0400, "Steven T. Hatton"
      <susudata@setid ava.kushan.aa> wrote:
      [color=blue]
      >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/isoc...tml#lib.vector
      > // lib.vector.capa city capacity:
      > size_type size() const;
      > size_type max_size() const;
      > void resize(size_typ e sz, T c = T());
      > size_type capacity() const;
      > bool empty() const;
      > void reserve(size_ty pe n);
      >
      >Is it reasonable for me to assume my vector /will/ stay put if I don't force
      >a reallocation by adding to it?[/color]

      The word you're interested here is "invalidate s". You wish to avoid
      anything that invalidates references, pointers and iterators into the
      vector. If you search the Standard for "invalidate s", 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:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • Ioannis Vranos

        #4
        Re: Can I trust a vector to stay put if I don't add to it?

        "Steven T. Hatton" <susudata@setid ava.kushan.aa> wrote in message
        news:9cqdnQNe_v d-QwfdRVn-ug@speakeasy.ne t...[color=blue]
        >
        > My understanding of the containers in the Standard Library is that they[/color]
        can[color=blue]
        > 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/isoc...tml#lib.vector
        > // lib.vector.capa city capacity:
        > size_type size() const;
        > size_type max_size() const;
        > void resize(size_typ e sz, T c = T());
        > size_type capacity() const;
        > bool empty() const;
        > void reserve(size_ty pe n);
        >
        > Is it reasonable for me to assume my vector /will/ stay put if I don't[/color]
        force[color=blue]
        > a reallocation by adding to it?[/color]


        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>::it erator p=a.begin()+3;

        *p=7;

        a.push_back(4);

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






        Regards,

        Ioannis Vranos

        Comment

        Working...