Understanding the Behavior of std::end() and Memory Allocation in Vectors

68 views Asked by At

What exactly does std::end() point to? When using a std::vector, does it allocate extra memory to accommodate the end position of the elements?

1

There are 1 answers

0
Remy Lebeau On

Calling std::end() on a std::vector simply returns the vector's end iterator, which points at the memory address beyond the last element of the vector's array:

image

If the vector's size is less than its capacity, there will be memory allocated for this 1-past-the-end position, but regardless, the content of the end position is undefined and should never be accessed.