Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about resizing vectors...

Reply
Thread Tools

about resizing vectors...

 
 
Lasse Skyum
Guest
Posts: n/a
 
      11-08-2003
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


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      11-08-2003
In article <3fad0870$0$27457$(E-Mail Removed)> ,
"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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-08-2003
"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.

 
Reply With Quote
 
Lasse Skyum
Guest
Posts: n/a
 
      11-09-2003


> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resizing a div by resizing its borders Pil (Trustworthy from Experience) Javascript 9 04-21-2009 07:35 AM
Resizing a div by resizing its borders Proper Javascript 0 04-18-2009 08:02 PM
Resizing for Firefox. Gary Walker Firefox 0 01-05-2005 07:42 PM
Browser resizing prevention? 'Captain' Kirk DeHaan Firefox 3 12-18-2004 10:01 PM
Resizing prefs window in Mozilla David Rodgers Firefox 3 08-26-2004 05:56 AM



Advertisments