Patrick Happel wrote:
> Hello all,
> I'm working with versions 4.1.2 and 4.1.0 of the STDCXX on HP-UX 11i
> with aCC 3.63, and I've noticed that when a std::vector is constructed
> with a size parameter, it's actually allocated as that size + 32
> additional elements. For example:
>
> #include
> #include
>
> int main() {
> std::vector testvec1(4);
> std::cout << "Capacity: " << testvec1.capacity() << std::endl;
> std::cout << "Size: " << testvec1.size() << std::endl;
> return 0;
> }
>
> When run will output:
>
> [phappel@moe 15d]$ ./stdvectest
> Capacity: 36
> Size: 4
>
> The HP-UX Standard Library will allocate 32 element sizes to a vector
> declared with a size less than 32, but will allocate as expected beyond
> that. STDCXX, however, seems like it will always add 32 to the size.
> I'm curious if there is an explanation for this - is it perhaps some
> quirk of memory allocation or template instantiation on HP-UX? Any
> insight would be appreciated!
Vector in stdcxx computes the capacity, C, from the desired
size N, the Golden Ratio R (1.618), and the minimum capacity
Cmin (32), using the following formula:
C := max (N * R, N + Cmin)
This is based on the assumption that more vectors will grow
than not, and that by how much they will grow is a function
of their current size. The Golden Ratio is known to strike
an optimum balance between the amount of utilized space and
the amount unused space. See, for example:
http://en.wikipedia.org/wiki/Golden_ratio
The HP implementation of vector (Rogue Wave libstd 2.2.1)
seems to use a simpler formula:
C := max (32, N)
The classic implementation of vector shipped by HP (Rogue Wave
libstd 1.2.1) as well as other popular implementations such as
GNU libstdc++ or IBM XLC++ simply use N:
C := N
Martin