If I declare a vector like so:
int main() {
vector<string> names;
int something_else_on_the_stack = 0;
names.add("John");
names.add("Annie");
}
How are you actually able to "add" elements to the names vector? If names is stack-allocated, shouldn't "something_else_on_the_stack" be right after it on the stack? Then how can you add names to the already allocated vector?
Internally, a vector<string>
will most likely consist of a string*
pointing at the actual data and probably two more size_t
members indicating occupied and reserved memory. All the rest will be on the heap. Therefore, sizeof(vector<string>)
is fixed, and the allocation on the stack won't change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With