By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

about resizing vectors...

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <3f***********************@dread16.news.tele.dk> ,
"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
Jul 19 '05 #2

P: n/a
"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.

Jul 19 '05 #3

P: n/a

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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.