Courses / Computer Science I
Data Structures

Arrays, Lists, and Memory Layout

Computer Science I 199 words Free to read

Memory Layout & Arrays

The two fundamental sequence structures differ in memory layout, and everything about their performance follows from it.

An array stores elements in a single contiguous block of memory. Because elements are adjacent and equally sized, the address of element ii is computed directly as base address+(i×element size)\text{base address} + (i \times \text{element size}), giving O(1)O(1) random access to jump to any index instantly.

The cost is rigidity: inserting or removing in the middle requires shifting all following elements, which takes O(n)O(n) time, and a fixed array cannot grow beyond its allocated size.

An array's cells sit touching in memory -- until something must be inserted

Linked Lists & Trade-offs

A linked list stores each element in a separate node holding a pointer to the next node. Nodes sit anywhere in memory; the chain is held together by pointers.

OperationArrayLinked list
Access element iO(1)O(1)O(n)O(n)
Insert/remove at known positionO(n)O(n)O(1)O(1)
Grow beyond capacityCostly / fixedEasy
Common pitfall: assuming linked lists are faster because insertion is O(1)O(1). That O(1)O(1) insertion assumes you already hold the position; finding it takes O(n)O(n), and arrays are far more cache-friendly.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes

Data Structures