- Do I correctly assume that the allocator is responsible for managing memory from the elements? (And make sure it is declared from stack / freestore / heap / whatever)
No no. A allocator is simply a sugar coating on top of new and delete and is generally responsible for deciding where the memory will be allocated. Responsibility for calling allocate , deallocate , construct and destruct belongs to its users (which means std::vector ). From your point of view, it will be automatic, which is important here in the end.
- What does std :: allocator do? - Is it declared from the stack or from the heap?
std::allocator is assigned using ::operator new(size_t) , so this depends on the definition of the global operator new . This usually means a bunch. The stack is intended for an object with automatic storage duration.
- (follow the previous question): if I copy an item declared on the stack to the data structure, is it still declared on the heap?
If you copy an element, the copy is highlighted where you copy it. Here, this means copying an item from the stack to the heap. Then you have two copies of the object, which means that the changes on one copy are not reflected on the other.
Beware, however, that this is the default copy mode, which is a shallow copy. If you copy an object, it will be fully copied; if you copy the pointer, only the pointer will be copied, not the data it points to.
Matthieu M. Nov 23 '10 at 10:19 2010-11-23 10:19
source share