How a growing array stays fast when it fills up
An array is a fixed block of memory, yet append keeps working and stays fast. The trick is spare room plus a rare, big copy. Here it is, one slot at a time.
01 An array cannot stretch in place
Indexing is instant because an array’s items sit side by side in one block of memory. That block has a fixed size. The bytes right after it may already hold something else, so the block cannot simply grow.
Yet Python’s list.append, Java’s ArrayList.add, C++’s vector::push_back and Go’s append all add to the end, again and again, and stay fast. They are all dynamic arrays: an ordinary array plus a little bookkeeping.
02 Length and capacity
A dynamic array keeps two numbers. Length is how many items you have stored. Capacity is how many slots the current block has room for. The block is usually bigger than it needs to be, so most appends just write into a spare slot.
- item stored
- spare slot
Only slots 0 to length − 1 are part of the array. Reading slot 6 here is an error, even though the memory exists.
03 The naive fix: grow by one slot
When the block is full, the array must get a bigger block and copy every item across. If it grew by just one slot each time, every append would copy everything. The 2nd append copies 1 item, the 3rd copies 2, and the millionth copies 999,999.
That adds up to about n² / 2 copies for n appends: roughly 500 billion for a million items.
04 What really happens: double when full
Real dynamic arrays grow by a factor. Watch an array of capacity 4 take five more items. Four appends are a single write each; one append pays for a move to a block twice the size.
- just written
- being copied
- spare slot
- not allocated yet
- freed
The array holds 7, 3, 9: length 3, capacity 4. One slot is spare. The next block does not exist yet.
05 Why the rare copy is cheap on average
Start from capacity 1 and double. The resizes copy 1, then 2, then 4, then 8 items, and so on. Each resize copies as many items as all the earlier resizes put together, plus one. So the total is always less than twice the final length.
| Appends (n) | Grow by one slot | Double when full | Resizes when doubling |
|---|---|---|---|
| 8 | 28 | 7 | 3 |
| 1,024 | 523,776 | 1,023 | 10 |
| 1,000,000 | 499,999,500,000 | 1,048,575 | 20 |
Spread over all the appends, that is fewer than 2 copies per append, however big the array gets. This is called amortised constant time: a single append can be slow, but the average over many appends is a small, fixed cost.
06 The factor each language uses
Any factor above 1 gives the same amortised result. A bigger factor means fewer copies but more empty slots; a smaller one wastes less memory but copies more often.
| Dynamic array | Grows to about | Note |
|---|---|---|
| C++ std::vector | 2× (GCC, Clang) | MSVC uses 1.5× |
| Java ArrayList | 1.5× | OpenJDK adds half the old capacity |
| Go slice (append) | 2×, then less | Eases towards 1.25× for large slices |
| CPython list | 1.125× plus a few slots | Small factor, so frequent but small resizes |
07 Watch a list grow in Python
sys.getsizeof reports the bytes a list object uses, including its block of pointers. Print it only when it changes, and you see each resize.
This is CPython 3.14 on a 64-bit machine, where each slot is an 8-byte pointer. The empty list is 56 bytes; the jumps show capacity going 0, 4, 8, 16, 24. Appends 2 to 4, 6 to 8 and 10 to 16 did not change the size at all: they wrote into spare slots. Other versions may print slightly different numbers.
08 When it bites
A resize moves every item to a new address. In C++, a pointer, reference or iterator into a std::vector is no longer valid after a push_back that reallocates. Using it afterwards is undefined behaviour, and it often seems to work until it does not.
int* p = &v[0];
v.push_back(x);
*p;Keeping a pointer across a pushIf that push reallocated, p points into the freed old block. Store an index instead, or call v.reserve(n) first so no reallocation happens.b := append(a, 4)Go slices that share memoryIf a has spare capacity, b uses the same block as a, and writes through one show up in the other. If not, b gets a new block. Which one you get depends on capacity, not on your intent.vector or Java ArrayList does not shrink when you remove items; use shrink_to_fit or trimToSize if it matters. A CPython list does shrink once it is less than half full.make([]int, 0, n)Not reserving when you know the sizeIf you know you will add n items, ask for the capacity up front: v.reserve(n) in C++, new ArrayList<>(n) in Java, make([]T, 0, n) in Go. Then no resize happens at all.