Understanding C++ vector memory allocation and capacity
This easy C++ question tests whether you understand how dynamic arrays (vectors) manage memory under the hood. It asks you to trace through a code snippet and predict its output—a skill that matters when you're reasoning about performance and debugging unexpected behaviour in systems code.
The question rewards familiarity with the distinction between a vector's size (number of elements currently stored) and its capacity (total allocated memory). When you add elements to a vector, it may trigger reallocations and growth strategies that aren't immediately obvious from the surface-level code. Tracing through the actual calls and state changes—rather than guessing—is what separates careful engineers from the rest.
- Size vs. capacity semantics
- Vector reallocation and growth factor
- How push_back() triggers memory management