In C++, the std::vector class provides a method called capacity() which returns the number of elements that the vector can hold before it needs to allocate more memory. The default behavior for std::vector::capacity() is influenced by multiple factors including default allocation, clearing the vector, and potential OS/memory management specific operations.
Here is a detailed explanation of the behavior:
1. Allocate an empty vector:
When you create an empty vector using std::vector<int> vec;, the initial capacity() of the vector is typically 0. This means that the vector has not pre-allocated memory for storing elements. Allocating elements to this vector will trigger dynamic memory allocation as needed. The initial capacity may occasionally be greater than 0 depending on the specific implementation of the standard library in use, but generally, it's safe to assume it starts at 0.
2. When the vector is cleared:
When you clear a vector using vec.clear();, the vector's size() becomes 0, but the capacity() remains unchanged. clear() removes all elements from the vector, but it does not deallocate the memory that was previously allocated:
std::vector<int> vec = {1, 2, 3, 4, 5}; // vec.size() = 5, vec.capacity() >= 5
vec.clear(); // vec.size() = 0, vec.capacity() remains >= 5
To reduce the capacity to free up memory, you would need to use shrink_to_fit() as follows:
vec.shrink_to_fit(); // `capacity` might shrink to minimum possible value (usually 0 for empty vector)
3. The possibility that the OS resizes the capacity of a vector for any reason:
The underlying std::vector implementation handles memory allocation using the allocator. The C++ standard does not provide a mechanism for the operating system (OS) to directly resize the capacity of a vector; however, the following scenarios can indirectly result in capacity adjustment:
-
Dynamic Allocation & Reallocation:
When elements are added beyond the current capacity, the vector dynamically reallocates its memory. This typically involves allocating a larger block of memory, copying existing elements to the new block, and then freeing the old block. This reallocation is determined by the implementation's strategy (often doubling the capacity or applying some growth factor).
-
Explicit Modification:
Operations like reserve() can be used to explicitly request a particular capacity. This can result in increasing the capacity size but never decreases it:
vec.reserve(100); // Guarantees that vec.capacity() >= 100
-
Allocator-Specific Behavior:
An allocator used by the vector may internally interact with the OS for memory management, but such interactions are encapsulated and not directly visible or controllable by the programmer.
Under normal circumstances, the OS does not autonomously change the capacity() of a vector; rather, it is managed internally by the vector's memory allocation algorithms and strategies.
In summary, the default behavior for std::vector::capacity() initially starts from zero for an empty vector, remains unchanged if the vector is cleared, and any changes to capacity typically result from internal allocation strategies, explicit requests like reserve(), or calls to methods like shrink_to_fit(). The OS does not autonomously or directly change the vector’s capacity.