Mental model
Memory is a long row of numbered bytes; a byte's number is its address. An array of n elements
of s bytes each occupies one block starting at some base address, so element i starts at
address(i) = base + i × s
That one multiplication is why indexing is O(1): no searching, no pointer to follow. A linked list
stores each element in its own node together with the address of the next node. To reach
element i you must read i addresses, one after another.
The lab places an array at teaching address 0x1000 and the list nodes at scattered addresses in a
heap region from 0x2000. Addresses are chosen for readability; real ones are larger and random.
Caches move lines, not bytes
Main memory is far slower than the processor, so CPUs keep recently used memory in small, fast caches. Data moves between memory and cache in fixed-size lines (64 bytes on most current desktop and server processors). Two effects follow:
- Spatial locality: reading one element brings its neighbours in with it. The next few array reads are then served from the cache.
- Temporal locality: data used recently is likely to still be cached when it is used again.
Hardware prefetchers also notice sequential access and fetch ahead, which helps arrays further.
Following next pointers defeats both: each node may sit on a different line, and the processor
cannot even start the next load until the current one has delivered the address.
The teaching model in the lab
The lab uses a deliberately simple model so every step can be shown:
- memory moves in lines of 16, 32 or 64 bytes, and an address belongs to line
⌊address ÷ line size⌋; - the cache holds 1 to 4 lines; on a miss, the least recently used line is evicted;
- a list node is the value plus an 8-byte pointer, with no allocator headers.
It counts line loads, not time. Real CPUs have several cache levels, different sizes and associativity, prefetchers and out-of-order execution. The model shows why contiguous data tends to need fewer loads; it does not predict how much faster any program runs.
In the model, an array of n elements of s bytes needs exactly ⌈n·s ÷ line⌉ loads (when it starts
on a line boundary, as in the lab). A list needs up to one load per node, depending on where the
nodes landed.
Big O does not see this
Traversing an array and traversing a linked list are both O(n). Big O counts operations and ignores the cost of each memory access, so two O(n) loops can behave very differently in practice. That is not a flaw in Big O; it is a reminder to state the cost model you are using.
Fragmentation
The free slots between the list nodes in the lab hint at fragmentation:
- External fragmentation: free memory is split into many small pieces, so a large request can fail (or need fresh memory) even though enough bytes are free in total.
- Internal fragmentation: a block is larger than what was asked for, because of alignment, padding or the allocator's size classes.
Compacting garbage collectors (the .NET GC, and most JVM collectors) move live objects together, which removes external fragmentation and can even place objects allocated one after another next to each other. A list built in one go in such a runtime is often fairly compact; after many insertions and removals it tends to scatter. C and C++ allocators never move objects.
Language notes
- C#:
T[]andList<T>keep elements contiguous; for value types such asintthe values themselves sit in the array. An array of a class type holds references, and the objects live elsewhere.LinkedList<T>allocates oneLinkedListNode<T>object per element. - Python: a
listis a contiguous array of references (in CPython, pointers to objects). The int objects themselves are scattered, so even a list behaves partly like linked data for its values.array.array,bytes,bytearrayand NumPy arrays keep raw numbers contiguous. - Java: an
int[]holds the numbers;ArrayList<Integer>holds references toIntegerobjects. The Java specification does not describe memory layout, but HotSpot stores array elements contiguously.LinkedListallocates a node object per element. - C++:
std::vector,std::arrayandstd::stringare contiguous containers by the standard:&v[i] == v.data() + i.std::listandstd::forward_listallocate every node separately;std::dequeuses fixed-size blocks.
Common mistakes
- Choosing a linked list for "fast insertion" when the program mostly traverses: finding the position is O(n) and pointer chasing is slow.
- Treating a Python list of numbers as compact numeric storage.
- Quoting a speed-up from a model or a micro-benchmark as if it held on every machine.
Edge cases
- An array of references is contiguous in its references only; the objects can be anywhere.
- Element sizes that do not divide the line size make some elements straddle two lines.
- With a cache of one line, even the array misses whenever it crosses into a new line, and nothing else.
Worked practice
1. An int array (4-byte elements) starts at address 4096. Where does element 10 start?
Answer
4096 + 10 × 4 = 4136.
2. In the teaching model with 32-byte lines, how many line loads does a traversal of 20 four-byte elements need, starting at 4096?
Answer
20 × 4 = 80 bytes, which spans ⌈80 ÷ 32⌉ = 3 lines: 3 loads.
Summary
- Contiguous storage makes addresses arithmetic and indexing O(1).
- Caches move whole lines, so neighbouring data comes along for free; linked nodes forfeit that.
- The lab's cache is a teaching model: it counts line loads and makes no timing claims.
- Fragmentation is wasted space between or inside blocks; compacting collectors reduce it.
- Contiguous built-ins: arrays,
List<T>,ArrayList(references),std::vector; Python'slistholds references,array.arrayholds values.