G
Guest
Sounds like something Knuth would have noticed, but I can't find it in
TAoCP. Recognising its Knuthiness and the use of the golden ratio, it
looks like a Fibonacci thing:
+-+
|X|
+-+
+---+-+
|X X| |
+---+-+
+-----+
|X X X|
+-----+
+---------+-----+
|X X X X X| |
+---------+-----+
+---------------+
|X X X X X X X X|
+---------------+
+-------------------------+---------------+
|X X X X X X X X X X X X X |
+-------------------------+---------------+
+-----------------------------------------+
|X X X X X X X X X X X X X X X X X X X X X|
+-----------------------------------------+
This seems like a good idea if you can use a smart implementation of
realloc. I have never written any allocators so I do not know much about
it but it was my belief that most implementations of realloc simply
looked to see if there was enough extra space "in front" to satisfy the
request (so that no data have to be copied), but I suppose there might
be some implementations that looks "behind" also.
However would that work in a C++ collection where the elements have to
be copy-constructed and simple copying wont work. It was my belief that
the container must first allocate the new memory, copy-construct the old
elements, add the new, and first then can it get rid of the old
allocation. To support a scheme like above the container must assume
that the two allocations overlap but to my knowledge the C++ allocators
can not allocate overlapping memory areas.
Perhaps another use for the ~ 1.6 ratio is to avoid unnecessary wasted
space in the form of over allocation?