Sub topic 7
The Problem with a Fixed Size
This chapter assumes you know basic programming (variables, loops, functions), introductory Big-O notation, and roughly what a memory address is. It builds on the previous chapters: an array stores its elements in one contiguous block, so indexing is O(1) and inserting in the middle requires shifting.
Why the size is fixed
A classic array is created with a length, and that length cannot change. The reason is the memory layout. The array occupies one unbroken block of memory. The bytes right after the block may already belong to another variable, another object, or the allocator's bookkeeping. The array cannot simply claim them.
Languages such as C, and Java's built-in int[], work this way. In Java, new int[5] always has length 5.
Two bad choices
When you create a fixed-size array, you must pick its length before you know how much data will arrive. Both kinds of mistake are costly.
- Too small. Once the array is full, the next element has nowhere to go. The program must fail, drop data, or build a bigger array by hand.
- Too large. Memory is reserved but unused. If you create many such arrays, the waste adds up.
Suppose you read lines from a file and store them. You do not know the line count in advance. Guessing 100 breaks on a 1,000-line file. Guessing 1,000,000 wastes memory on a 10-line file.
What happens when it is full
Here is a fixed-capacity array written in Python, which has no such limit in its own lists. The class stores items in a preallocated block and rejects an append when the block is full.
class FixedArray:
def __init__(self, capacity):
self._capacity = capacity
self._size = 0
self._data = [None] * capacity
def append(self, value):
if self._size == self._capacity:
raise OverflowError("array is full")
self._data[self._size] = value
self._size += 1
def __len__(self):
return self._size
arr = FixedArray(3)
for n in (10, 20, 30):
arr.append(n)
print(len(arr))
try:
arr.append(40)
except OverflowError as error:
print("error:", error)The program prints 3 and then error: array is full. The fourth append fails even though the machine has plenty of free memory elsewhere. The limit comes from the array's design, not from a lack of memory.
The manual fix and its cost
The workaround is to allocate a larger block, copy every element into it, and then use the new block. Copying n elements costs O(n).
If you do this by hand each time the array fills, you have two new decisions to make: how much larger the new block should be, and how often you are willing to pay for a copy. Growing by one slot each time makes every append trigger a full copy. Appending n items then costs about 1 + 2 + ... + n copies, which is O(n²) in total.
What we want instead
The goal is an array that keeps O(1) indexing and a contiguous layout, yet accepts new elements without a preset limit and without wasting memory on a large guess. That structure is the dynamic array, introduced next. It hides the allocate-and-copy step behind a simple append operation.
Exercise
A program reads an unknown number of sensor readings into a fixed-size array of length 100. Name two problems this design can cause, and explain why the array cannot just grow in place.
Show answer
If more than 100 readings arrive, the array overflows and data is lost or the program fails. If only a few readings arrive, most of the reserved memory is wasted. The array cannot grow in place because it is one contiguous block, and the memory just past its end may already be used by other data.
Exercise
An array grows by exactly one slot whenever it is full, copying all elements each time. Roughly how many element copies happen in total while appending 1,000 items to an initially empty array?
Show answer
About 1 + 2 + ... + 999, which is roughly 500,000 copies (n(n-1)/2). This is O(n²) in total, so each append costs O(n) on average.