I’m defining a vector as:
vector< int, MyAlloc< int> > *v = new vector< int, MyAllooc< int> > (4);
MyAlloc is allocating space for only 4 ints. Memory for _M_start, _M_finish, and _M_end_of_storage is being allocated on the heap before the memory for the 4 ints. But who is allocating this memory for _M_start, _M_finish, and _M_end_of_storage? I want to allocate this memory myself. What do I have to do?
When you create the vector, it allocates room for the vector’s member variables (the
_Mones) wherever you place the vector. If you usenew, it allocates space for these variables on the heap. With local variables, the compiler makes space for them in the current stack frame. If you make the vector a member of a class, the compiler makes space for them in the containing class.The vector then uses its allocator to allocate room for the data you wish to store in the vector, using whatever mechanism the allocator is defined to use.