Why can’t pop_front() be implemented for C++ vectors by simply shifting the pointer contained in the vector’s name one spot over? So in a vector containing an array foo, foo is a pointer to foo[0], so pop_front() would make the pointer foo = foo[1] and the bracket operator would just do the normal pointer math. Is this something to do with how C++ keeps track of the memory you’re using for what when it allocates space for an array?
This is similar to other questions I’ve seen about why std::vector doesn’t have a pop_front() function, I will admit, but i haven’t anyone asking about why you can’t shift the pointer.
The
vectorwouldn’t be able to free its memory if it did this.Generally, you want the overhead per
vectorobject to be small. That means you only store three items: the pointer to the first element, the capacity, and the length.In order to implement what you suggest, every
vectorever (all of them) would need an additional member variable: the offset from the start pointer at which the zeroth element resides. Otherwise, the memory could not be freed, since the original handle to it would have been lost.It’s a tradeoff, but generally the memory consumption of an object which may have millions of instances is more valuable than the corner case of doing the absolute worst thing you can do performance-wise to the
vector.