Your question could be simplified. Consider:
int main()
{
std::vector<int> v;
v.push_back(42);
}
"Where is 42
stored?"
In this example, v
is an automatic variable, so it is stored in the local scope's store (usually the stack). But note that v
has a fixed, small size -- it's just the handler object. The actual memory for the contained elements is separately allocated by the vector's internal logic, using the specified allocator (in this case, std::allocator
), which in turn takes all its memory from the free store.
All the (non-static) data members of a class form part of that class's data type and consume space -- this is essentially the same as a dumb C struct
. However, the magic of most C++ library classes lies in the fact that their data members are only tiny pointers, and all the payload data is allocated separately and managed by the class's internal logic.
So when you make a class with lots of vector members, the class itself still won't be very big. Any instance of that class will be allocated in its entirety wherever and however you want it (automatically, dynamically, statically), but be aware that each member object will request additional, separate memory all by itself to do its work.