473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

about resizing vectors...

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
3 2005
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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bernhard Gsöllpointner | last post by:
Hello ! Does anyody know how to load and resize jpg or gif files in a cpp 6.0 Project ? Thanks in advance bernhard.g@utanet.at
2
by: Sean Dettrick | last post by:
Hi, Can anyone please tell me if the following is possible, and if so, what is the correct syntax? I'd like to resize a vector (and access other vector member functions) via a pointer to that...
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
7
by: Markus Pitha | last post by:
Hello again, I have another question about the handling of multidimensional vectors. Actually, it's not difficult when the size is known at the beginning, but I still wasn't able to create...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.