I want to implement a container. The data will be stored in a dynamically allocated array. I need advice on memory reallocation.
Basically, i want a formula on how much bigger i should make the array when it is full. I think a constant value would be sub-optimal, since the larger the array is, the longer it takes to copy it.
For example, if an array can store 1000000 doubles and it becomes full, reallocating for 1000005 doubles would be stupid. Going for 1001000 would be a better idea. On the contrary, if i have an array of 5 doubles and it gets full, enlarging it to 1005 units is equally stupid. Maybe enlarging it by 10% (or like by 20+10% so that it feels ok on small arrays too) every time would be a better idea. Any advice on this?
std::vector<>implementations double the current capacity of the vector when reallocation is needed.std::vector). Current implementations seem to use a factor of 1.5 more often than 2, but the same basic idea still applies.