reserve() in vector?

Discussion in 'C++' started by wenmang@yahoo.com, Oct 3, 2006.

  1. Guest

    hi,
    I like to preallocate a vector of an element X, my question is after
    calling reserve(), is there a guarantee that memory is contiguous?
     
    , Oct 3, 2006
    #1
    1. Advertising

  2. wrote:
    > I like to preallocate a vector of an element X, my question is after
    > calling reserve(), is there a guarantee that memory is contiguous?


    Yes.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 3, 2006
    #2
    1. Advertising

  3. red floyd Guest

    Victor Bazarov wrote:
    > wrote:
    >> I like to preallocate a vector of an element X, my question is after
    >> calling reserve(), is there a guarantee that memory is contiguous?

    >
    > Yes.


    Specifically 23.2.4/1 (ISO/IEC 14882:2003).

    "The elements of a vector are stored contiguously, meaning that if v is
    a vector<T, Allocator> where T is some type other than bool, then it
    obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."
     
    red floyd, Oct 3, 2006
    #3
  4. Alan Johnson Guest

    wrote:
    > hi,
    > I like to preallocate a vector of an element X, my question is after
    > calling reserve(), is there a guarantee that memory is contiguous?


    As others have said, the memory is guaranteed to be contiguous.
    However, from the wording of your question it sounds like you may
    misinterpret the intent of the reserve function. Are you trying to do
    something like this?

    void some_library_function(char * p, size_t len)
    {
    // Write to p.
    }

    int main()
    {
    std::vector<char> v ;
    v.reserve(100) ;
    some_library_function(&v[0], 100) ; // ERROR!!
    }

    Calling reserve will allocate enough memory to hold the specified
    number of objects, but it doesn't give you access to that memory. That
    is, after a call to reserve, the vector still contains the same number
    of elements as before to call to reserve. If your intent is to do
    something like the above, use resize instead of reserve.

    --
    Alan Johnson
     
    Alan Johnson, Oct 4, 2006
    #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 smith

    vector.reserve

    john smith, Jul 25, 2003, in forum: C++
    Replies:
    5
    Views:
    695
    John Harrison
    Jul 25, 2003
  2. Alex Vinokur

    vector : reserve(LONG_MAX)

    Alex Vinokur, Apr 22, 2004, in forum: C++
    Replies:
    2
    Views:
    449
    Pete Becker
    Apr 22, 2004
  3. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    468
    Victor Bazarov
    Feb 27, 2005
  4. Gary Kuehn
    Replies:
    2
    Views:
    463
    Gary Kuehn
    Jul 19, 2005
  5. Replies:
    8
    Views:
    1,932
    Csaba
    Feb 18, 2006
Loading...

Share This Page