C/C++ Arena

How a vector grows

A std::vector keeps its elements in a heap block. Size is how many you've added; capacity is how many fit before it has to grow.

Watch the heap during push_back: when the block is full, the vector allocates a bigger one, moves the elements over, and frees the old one. That's why pointers and iterators into a vector can break after a push_back, and why reserve helps when you know the final size.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;
    for (int i = 1; i <= 5; i++) {
        v.push_back(i * 10);
    }
    std::cout << v.size() << " " << v.capacity() << "\n";
    return 0;
}

Output:

5 8

From the lesson: vector and string