From this answer, I am led to believe that the members of a class are guaranteed to be contiguous in memory, in the order that they are declared. However, from the docs, "vectors use contiguous storage locations for their elements". So then how is it possible to have multiple vectors in a class, or a vector that is not the last member of a class, given that a vector may resize and spill over into memory that is already allocated?
Asked
Active
Viewed 132 times
2 Answers
6
std::vector
is usually implemented as three pointers. One pointer to a dynamically allocated array that stores the vector
's contents, one pointer to the end of the used memory and one pointer to the end of the allocated array. No matter how the vector
is allocated, the vector
's data is stored elsewhere in dynamic storage.

user4581301
- 33,082
- 7
- 33
- 54