473,473 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Vector reallocation

When you insert an element into a vector, if the internal memory is already
used, how does the vector allocate additional memory? It just allocates one
unit to satisfy the insert, or allocates a chunk of memory? I guss the
vector will allocate a chunk of memory. If this is true, what algorithm
does the vector use? How does vectors decide how big the chunk of memory to
create?

Thanks in advace!
Jul 22 '05 #1
9 2948
newbiecpp wrote:
When you insert an element into a vector, if the internal memory is already
used, how does the vector allocate additional memory? It just allocates one
unit to satisfy the insert, or allocates a chunk of memory? I guss the
vector will allocate a chunk of memory. If this is true, what algorithm
does the vector use? How does vectors decide how big the chunk of memory to
create?


The vector allocates a chunk to keep the entire sequence of all elements
(and a bit more, in case you want to extend it) using its "Allocator"
template argument (you mostly use the default allocator, of course). It
uses straightforward "new[]", usually. The size is decided based on the
number of elements to keep or based on the argument for the 'reserve'
member (if you called it).

If the storage already has enough room, no reallocation happens, but the
elements are "shifted down" one position by means of the assignment op,
IIRC.

Victor
Jul 22 '05 #2
On Mon, 26 Jul 2004 19:52:46 GMT, newbiecpp <ne*******@yahoo.com> wrote:
When you insert an element into a vector, if the internal memory is
already
used, how does the vector allocate additional memory? It just allocates
one
unit to satisfy the insert, or allocates a chunk of memory? I guss the
vector will allocate a chunk of memory. If this is true, what algorithm
does the vector use? How does vectors decide how big the chunk of
memory to
create?

Thanks in advace!


Typical strategy is to double the allocated memory. This means that
reallocations get rarer as the vector gets bigger. In any case the
reallocation must be some multiple of the size of the existing memory. It
is not possible to satisfy the efficiency requirements of a vector by
adding a fixed amount of memory.

John
Jul 22 '05 #3
John Harrison wrote:
...
When you insert an element into a vector, if the internal memory is
already
used, how does the vector allocate additional memory? It just allocates
one
unit to satisfy the insert, or allocates a chunk of memory? I guss the
vector will allocate a chunk of memory. If this is true, what algorithm
does the vector use? How does vectors decide how big the chunk of
memory to
create?

Thanks in advace!


Typical strategy is to double the allocated memory. This means that
reallocations get rarer as the vector gets bigger. In any case the
reallocation must be some multiple of the size of the existing memory. It
is not possible to satisfy the efficiency requirements of a vector by
adding a fixed amount of memory.
...


The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4
Andrey Tarasevich wrote:
The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


So in essence, it uses a Fibonacci allocation. (The ratio of
Fib(n)/Fib(n-1) approaches the (1+sqrt(5))/2) as n approaches infinity).
Jul 22 '05 #5
"red floyd" <no*****@here.dude> wrote...
Andrey Tarasevich wrote:
The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


So in essence, it uses a Fibonacci allocation. (The ratio of
Fib(n)/Fib(n-1) approaches the (1+sqrt(5))/2) as n approaches infinity).


In essence it doesn't. It has to use something below it. And it
is not a "Fibonacci allocation". It's rather a "golden section"
allocation, wouldn't you agree? The ratio was known to ancients
way before Fibonacci defined his sequence.

V
Jul 22 '05 #6
>
The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


Any chance of a link, or an indication of where that article might have
been published?

john
Jul 22 '05 #7
>
The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


When you say 'typical implementation' are you talking about implementation
of the heap or implementation of vector?

john
Jul 22 '05 #8
John Harrison wrote:

The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


Any chance of a link, or an indication of where that article might have
been published?


Take a look at this thread in comp.lang.c++.moderated

http://www.google.com/groups?thl=0,1...anet.com#link1

It gives a pretty exhaustive explanation by itself. It also mentions the
article, but somehow I can't find more precise info about it in the
thread (although I seem to remember that someone actually mentioned the
name and the issue of the magazine where it was published).

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #9

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsbr76ymf212331@andronicus...

The strategy with _doubling_ the memory has certain flaws (IIRC, Andrew
Koenig wrote an article about this some time ago). Multiplying the
capacity by any value above (1 + sqrt(5))/2 might lead to extremely
inefficient memory usage in typical implementation. AFAIK, modern
version of STL tend to use 1.6 as a multiplier value.


Any chance of a link, or an indication of where that article might have
been published?

john


Answering my own questions again, but here is a link

http://groups.google.com/groups?q=g:...rldnet.att.net

It's a clever argument but I would have to be convinced its an issue in the
real world.

john
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
8
by: Joseph Turian | last post by:
Fellow hackers, Is it possible to shrink the capacity of a vector? i.e. Without new'ing and delete'ing a vector, can one return its memory to the heap? Here's what I get under the g++...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
7
by: linq936 | last post by:
Hi, The following is a psudo code to describe my question: vector<int> ints; vector<int>::iterator itr; while (itr != ints.end()){ int j = some_function(*i); if (j>0){ ints.push_back(j); }
9
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
29
by: ab2305 | last post by:
Does standard mandates that the reserve call should initialize a container's values to its defaults, hence vector<intintVec; intVec.reserve(100) would make intVec=intVec....intVec=0 ? Thanks
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
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.