The free Member
The free member must destroy the elements and then deallocate the space this StrVec allocated. The for loop calls the allocator member destroy in reverse order, starting with the last constructed element and finishing with the first:
void StrVec::free() { // may not pass deallocate a 0 pointer; if elements is 0, there's no work to do if (elements) { // destroy the old elements in reverse order for (auto p = first_free; p != elements; /* empty */) alloc.destroy(--p); alloc.deallocate(elements, cap - elements); } }
The destroy function runs the string destructor. The string destructor frees whatever storage was allocated by the strings themselves.
Once the elements have been destroyed, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access