Skip to content

Latest commit

 

History

History
14 lines (10 loc) · 620 Bytes

clear-cpp-vector.md

File metadata and controls

14 lines (10 loc) · 620 Bytes

Clearing a C++ vector (and memory it has allocated)

I was recently having a hard time tracking down a memory leak in one of my applications. The documentation for clear is not very clear about the fact that after calling clear, the compiler is not guaranteed to cleanup the space allocated.

There are two ways to handle cleaning up the memory allocated by a vector in this case:

// Shrink method
my_vec.clear();
my_vec.shrink_to_fit(); // Capacity is now 0, memory cleared

// Swap method
vector< int >().swap( my_vec ); // Capacity is now 0