Dynamic array usually have a growth factor of 3/2 to 2. But once the memory is allocated, it is never shrank automatically. Would it be pertinent to have a decay factor let's say twice as big as the growth factor? I mean if the number of elements is N times smaller than the decay factor, the array is reallocated (realloc
) with a smaller size?
I found plenty of information about dynamic arrays growth, but nothing about the opposite operation.
std::vector
and have explicit shrink-to-fit and reserve functions.