Since container data types have dynamic size I’m assuming they allocate memory on the heap. But when/how do they free this allocated memory?
Since container data types have dynamic size I’m assuming they allocate memory on the
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.
They get freed either when they go out of scope (if the container was created in the stack), or when you explicitly call delete on the container(in case of heap-based container). When this happens, the destructor of the container automatically gets called and the heap memory allocated for the container (that contains the data) are freed then.
Simply removing an element in the container won’t necessarily free the memory right away, since STL containers generally use caching to speed things up. Remember, new/delete operations are relatively costly.