Skip to content

How a growing array stays fast when it fills up

Working knowledgeUnder the hood

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.

Python
xs = []
for i in range(1_000_000):
xs.append(i) # about constant time each, on average

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.

Length 5, capacity 8: three appends can happen before anything moves
One block of 8 slots
70
31
92
43
84
5
6
7
  • 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.

Grow by one slot≈ n² / 2 copiesEvery append copies the whole array. Twice the items, four times the work.
Double the capacityfewer than 2n copiesA resize copies a lot, but the next one is twice as far away.

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.

Five appends, one resize
Step 1 of 11
Capacity 4Items copied 0
Old block · capacity 4
70
31
92
3
New block · capacity 8
0
1
2
3
4
5
6
7
  • 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.

3 length

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.

Total items copied for n appends, starting from capacity 1
Appends (n)Grow by one slotDouble when fullResizes when doubling
82873
1,024523,7761,02310
1,000,000499,999,500,0001,048,57520

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.

Growth when full (typical current versions)
Dynamic arrayGrows to aboutNote
C++ std::vector2× (GCC, Clang)MSVC uses 1.5×
Java ArrayList1.5×OpenJDK adds half the old capacity
Go slice (append)2×, then lessEases towards 1.25× for large slices
CPython list1.125× plus a few slotsSmall 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.

grow.py
import sys
xs = []
last = sys.getsizeof(xs)
print(f"len 0: {last} bytes")
for i in range(20):
xs.append(i)
size = sys.getsizeof(xs)
if size != last:
print(f"len {len(xs)}: {size} bytes")
last = size
Shell
$ python3 grow.py
len 0: 56 bytes
len 1: 88 bytes
len 5: 120 bytes
len 9: 184 bytes
len 17: 248 bytes

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.
One append can be slowThe average is cheap, but the append that triggers a resize copies everything. With a million items, that single call moves a million. In code with tight time limits per call, size the array up front.
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.
Spare memory that staysRight after a doubling, up to half the slots are empty. A C++ 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.

09 Check yourself

Question

A std::vector has length 8 and capacity 8, and it doubles when full. You call push_back once. How many existing items are copied?

Question

An empty array that doubles from capacity 1 takes 1,000,000 appends. About how many item copies happen in total?

Question

Instead of doubling, an array grows by 100 slots each time it is full. What happens to the cost of n appends?

10 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?