about resizing vectors...

Discussion in 'C++' started by Lasse Skyum, Nov 8, 2003.

  1. Lasse Skyum

    Lasse Skyum Guest

    Is it true that when std::vector resizes for more capacity it copies all the
    elements to a bigger array and then destroys all the elements from the old
    one?

    If so, why doesn't it just use realloc for possible resizing of the existing
    memoryblock??

    --
    Lasse
     
    Lasse Skyum, Nov 8, 2003
    #1
    1. Advertising

  2. In article <3fad0870$0$27457$>,
    "Lasse Skyum" <no spam> wrote:

    > Is it true that when std::vector resizes for more capacity it copies all the
    > elements to a bigger array and then destroys all the elements from the old
    > one?


    Yes.

    > If so, why doesn't it just use realloc for possible resizing of the existing
    > memoryblock??


    The most fundamental reason is that in general vector's contained
    element can not be copied using memcpy (as realloc threatens to do).
    Instead the element's copy constructor must be used.

    That being said, I believe it is a worthwhile goal to teach vector to
    attempt to expand in place when it needs to increase its capacity.

    -Howard
     
    Howard Hinnant, Nov 8, 2003
    #2
    1. Advertising

  3. Lasse Skyum

    Rolf Magnus Guest

    "Lasse Skyum" <no spam> wrote:

    > Is it true that when std::vector resizes for more capacity it copies
    > all the elements to a bigger array and then destroys all the elements
    > from the old one?


    Yes.

    > If so, why doesn't it just use realloc for possible resizing of the
    > existing memoryblock??


    Realloc basically does the same, but would only work correctly with POD
    types, since it's not aware of classes and their copy constructors and
    destructors.
     
    Rolf Magnus, Nov 8, 2003
    #3
  4. Lasse Skyum

    Lasse Skyum Guest


    > > If so, why doesn't it just use realloc for possible resizing of the

    existing
    > > memoryblock??

    >
    > The most fundamental reason is that in general vector's contained
    > element can not be copied using memcpy (as realloc threatens to do).
    > Instead the element's copy constructor must be used.


    Okay, I understand...

    > That being said, I believe it is a worthwhile goal to teach vector to
    > attempt to expand in place when it needs to increase its capacity.


    Yes, but nevermind... I'm just studing how it's all working and don't really
    need to optimize this :)

    --
    Lasse
     
    Lasse Skyum, Nov 9, 2003
    #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. Anand P Paralkar
    Replies:
    2
    Views:
    9,540
    Srinivasan Venkataramanan
    Aug 4, 2003
  2. Replies:
    3
    Views:
    725
    Shadowman
    Mar 26, 2008
  3. Guest
    Replies:
    0
    Views:
    481
    Guest
    Sep 14, 2005
  4. Pil (Trustworthy from Experience)

    Resizing a div by resizing its borders

    Pil (Trustworthy from Experience), Apr 18, 2009, in forum: Javascript
    Replies:
    9
    Views:
    370
    Proper
    Apr 21, 2009
  5. Proper
    Replies:
    0
    Views:
    211
    Proper
    Apr 18, 2009
Loading...

Share This Page