vector::reserve,

Discussion in 'C++' started by quat, Apr 29, 2006.

  1. quat

    quat Guest

    Hi,

    Is it not legal to access an element that has been reserved?

    For example:

    vector<int> x;
    x.reserve(10);

    x[0] = 1;

    This is throwing an exception with VS 2005, but worked fine for VS 2002.

    From what I understand, reserve allocated memory but does not adjust the
    internal "size" member. So technically, I should be able to access the
    elements?
     
    quat, Apr 29, 2006
    #1
    1. Advertising

  2. quat

    Rolf Magnus Guest

    quat wrote:

    > Hi,
    >
    > Is it not legal to access an element that has been reserved?


    Yes, it is not.

    > For example:
    >
    > vector<int> x;
    > x.reserve(10);
    >
    > x[0] = 1;
    >
    > This is throwing an exception with VS 2005, but worked fine for VS 2002.
    >
    > From what I understand, reserve allocated memory but does not adjust the
    > internal "size" member.


    Reserve allocates memory, but doesn't create any objects. The above vector
    contains 0 objects of type int, but you can add (e.g. with push_back) at
    least 10 without a reallocation happening. You can't access any objects
    yet, because there are still none in the vector. Whatever "internal"
    members a vector might or might not have is of no relevance for that.
    That's an implementation detail you aren't supposed to care about.

    > So technically, I should be able to access the elements?


    No, because there are no elements yet. If you want a vector with 10
    elemenents, try using resize() instead of reserve().
     
    Rolf Magnus, Apr 29, 2006
    #2
    1. Advertising

  3. quat

    quat Guest


    > No, because there are no elements yet. If you want a vector with 10
    > elemenents, try using resize() instead of reserve().
    >


    I can accept this. However, I don't get how an allocation can occur without
    creating objects. Presumably, reserve does something like this:

    data = new type[n]; // where n = amount to reserve.

    Thus data[j] should be accessible for 0 <= j < n.
     
    quat, Apr 29, 2006
    #3
  4. quat

    Rolf Magnus Guest

    quat wrote:

    >
    >> No, because there are no elements yet. If you want a vector with 10
    >> elemenents, try using resize() instead of reserve().
    >>

    >
    > I can accept this. However, I don't get how an allocation can occur
    > without
    > creating objects. Presumably, reserve does something like this:
    >
    > data = new type[n]; // where n = amount to reserve.


    There are ways of allocating raw memory without creating objects (using
    global operator new), and there are ways of turning that raw memory (using
    placement new).

    > Thus data[j] should be accessible for 0 <= j < n.


    It might be, but there is no guarantee, unless you actually resize the
    vector.
     
    Rolf Magnus, Apr 29, 2006
    #4
  5. quat

    quat Guest


    > There are ways of allocating raw memory without creating objects (using
    > global operator new), and there are ways of turning that raw memory (using
    > placement new).


    Okay I didn't know this. Thanks.
     
    quat, Apr 29, 2006
    #5
  6. quat

    Ron Natalie Guest

    quat wrote:
    >> There are ways of allocating raw memory without creating objects (using
    >> global operator new), and there are ways of turning that raw memory (using
    >> placement new).

    >
    > Okay I didn't know this. Thanks.
    >
    >

    If you want to create elements in a vector en-masse, use
    resize() not reserve().
     
    Ron Natalie, May 1, 2006
    #6
    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:
    713
    John Harrison
    Jul 25, 2003
  2. Alex Vinokur

    vector : reserve(LONG_MAX)

    Alex Vinokur, Apr 22, 2004, in forum: C++
    Replies:
    2
    Views:
    465
    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:
    483
    Victor Bazarov
    Feb 27, 2005
  4. Gary Kuehn
    Replies:
    2
    Views:
    495
    Gary Kuehn
    Jul 19, 2005
  5. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
Loading...

Share This Page