Summary
In this chapter, we have thoroughly examined the relationship between memory management and the use of std::vector
. We began by revisiting the fundamental concepts of capacity versus size, emphasizing their distinct roles and the importance of this distinction for efficient memory use. The mechanics underlying the std::vector
container’s memory allocation were then explored, clarifying what happens internally when vectors grow or shrink.
We discussed the nuances of resizing and reserving memory, where functions such as reserve()
and shrink_to_fit()
were introduced as tools for optimizing memory usage. The real-world relevance of these methods was underscored, highlighting their utility in high-performance applications.
The chapter introduced the basics of custom allocators, elaborating on their role and delving into the allocator interface. We addressed the trade-offs and illustrated why custom allocators can be preferable to directly using new
, delete
, and managed...