How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Perhaps some additions to Steve’s nice answer:
Indeed, internal allocation is done by allocators, which serve two separate purposes: Allocating and releasing memory, and constructing and destroying objects. Objects are always (copy or move) constructed on
insertand destroyed onerase, however, the interna vary.Node-based containers will typically allocate and construct an entire internal node, which contains both the actual object and bookkeeping data (like the next/prev pointers in a doubly-linked list). When you erase one of those, the container will destroy the object and release the memory.
Sequence containers like vector will strictly separate allocation and construction; the amount of memory that’s been allocated will typically only grow, but when you erase (after the erased object’s destructor has been called), the other elements have to be moved to maintain contiguous memory layout.
The internal allocator work may look quite different from your usual
new/deletework if you haven’t seen it before, but ultimately there’s always a construction and a destruction somewhere.