vector
cannot free its memory if it does.
Typically, you want the overhead of a vector
object to be small. This means that you save only three elements: a pointer to the first element, capacity and length.
To implement what you are proposing, for each vector
ever ( all ) an additional member variable is required: the offset from the start pointer where the null element is located. Otherwise, the memory cannot be freed, since the original descriptor to it would be lost.
This is a compromise, but overall, the memory consumption of an object, which can have millions of instances, is more valuable than the corner case when you do the absolute worst, which you can do in terms of vector
performance.
source share