liveBook · Manning

Fixed-size array

As explained in Grokking Data Structures

A static array is an array whose size is fixed when it is created and whose storage is allocated for that fixed number of elements.

Imagine a row of numbered drawers: each drawer holds one value, and its number tells you where to find it. A fixed-size array works much the same way. It stores a collection of elements accessible by index, but you must choose the number of positions when you create it. That number stays fixed for the array’s lifetime. section 2.1 section 2.19

A static array typically holds elements of one type. Equal-sized elements can sit side by side in one block of memory, so the location of an indexed element is quick to determine. In Python-style zero-based indexing, an array of size n has valid positions from 0 through n−1; position n is out of bounds. section 2.3 section 2.4

Arrays implementation and memory addresses

The memory-address diagram shows why equally sized elements can be reached directly by index. section 2.4

The fixed capacity shapes everyday operations:

  • Choose capacity and element type at creation. You may assign initial values then; if you do not, check your language’s rules before reading an element. section 2.5
  • Read or write a position by its index without visiting earlier elements. To find an element when you do not know its index, traverse the array. section 5.19 section 2.19
  • If order does not matter and space is available, adding or removing an element can be straightforward. Once the array is full, storing more requires a new, larger array and copying elements into it. section 2.19 section 5.2

A calendar with numbered positions

An array can hold values for 24 days. With zero-based indexes, the value for day 1 sits at index 0, and day 24 sits at index 23. The known number of days makes fixed capacity convenient; the offset is worth remembering. section 2.3 section 2.8

Pick a fixed-size array when the required capacity is known or stable. Reserving far more positions than you usually need wastes memory; reserving too few forces a costly replacement. A dynamic array manages that replacement for you, using fixed-size arrays underneath. Fixed capacity can also suit a stack or a queue whose maximum size is predetermined. section 5.2 section 5.3 section 8.6 section 9.9

In Python, a list can resize, and array.array is typed but still dynamic. To experiment with fixed-size behavior, use a custom Array simulator; NumPy also offers fixed-size arrays. Do not assume an uninitialized position has a particular value without checking the language’s rules. section 2.7 section 2.5

How Grokking Data Structures covers it

How deep it goes
Explains capacity, indexing, memory layout, initialization, and resizing costs, then supports experimentation with a custom Python class.
Examples
Uses an Advent calendar for indexing and a preallocated queue to illustrate when fixed capacity is acceptable.
What it assumes
Assumes familiarity with variables while introducing memory organization through drawers and explaining indexes from the beginning.
What you can do after
Apply it via a custom Python Array simulator and choose fixed or dynamic capacity according to expected demand.
How it connects
Connects fixed-size arrays to dynamic arrays, stacks, linked lists, and circular queues through storage and capacity tradeoffs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.