What is the basis for memory management in standard container classes (vector, stack)? realloc
?
2 answers
Memory management, in standard containers, lies on the shoulders of the allocators, if none is passed, then the standard one, which uses new
and delete
, is used. No realloc
. If there is not enough memory, then we allocate a new one and copy (move) everything from the old to the new, and delete the old (this is the behavior of the vector, not all containers work in the same way)
A vector and a stack, generally speaking, are very different containers (the stack is not a container at all, but an adapter above the deck). And the approaches to memory management are very different.
However, none of the standard containers use realloc()
(as well as malloc()
) for obvious reasons - realloc()
does not know anything about constructors.