Base CS from zero
Arrays as contiguous cells
So far every variable you have seen has held a single value: one number, one boolean, one string. But a program almost always needs to hold many values of the same kind — the scores of every player, the bytes of a file, the pixels of an image.
You could declare a hundred separate variables, but that is unworkable. Instead, languages give you the array: one name that refers to a whole sequence of values laid out in order. The first thing to understand about an array is not its syntax — it is its shape in memory. An array is not scattered across memory at random. It is a single unbroken run of cells, each the same size, placed directly back-to-back.
After this lesson you will be able to picture exactly where each element of an array sits in memory, and why that picture is the foundation for everything arrays can do.
After this lesson you can define an array as a contiguous run of equal-size cells, explain why every cell in an array is the same size, identify which addresses a small array occupies, and trace the memory layout of an array literal element by element.
Recall two facts from earlier units. From Unit 02: memory is a long row of addressable cells, each with its own numeric address, and the byte (8 bits) is the standard unit a cell holds. From Unit 05: every value has a type, and the type fixes how many bytes the value needs — a number of a given type always takes the same fixed number of bytes.
An array is built directly on those two facts. An array is a sequence of values, all of the same type, stored in memory as a run of cells that are:
- Equal in size. Because every element has the same type, every element needs the same number of bytes. So every cell in the array is the same size. Call that size the element size.
- Contiguous. The cells sit directly back-to-back, with no gaps between them. The array occupies one unbroken block of addresses. This property is called contiguous memory.
Each value stored in the array is called an element. If an array has 5 elements and each element is 4 bytes, the whole array is one solid block of 5 × 4 = 20 bytes.
The two properties are linked. Equal-size is what makes contiguous useful: when identical cells are packed back-to-back, the position of any cell is perfectly regular and predictable. You will use that regularity in the next lesson to find any element instantly. For now, the goal is just to see the shape: an array is a tidy, gapless strip of identical cells.
The program below builds a small array of 5 numbers. The trace shows the array taking shape one cell at a time.
1
// An array literal: 5 numbers of the same type.
2
let scores: number[] = [90, 80, 70, 60, 50];
3
4
// All five values live in one contiguous block of memory.
5
// Element 0 is at the start of the block; the rest follow
6
// directly behind it, each in a cell of the same size.
7
8
// scores has 5 elements.
9
// If each number takes 4 bytes, the whole array is 20 bytes.
- L2 scores is an array of 5 elements, every element a number — so every cell is the same size
- L5 Element 0 (value 90) sits first; element 1 (value 80) sits directly behind it, and so on
- L6 No gaps: the cells are back-to-back. This is contiguous memory.
- L9 5 elements x 4 bytes each = one solid 20-byte block
Step through building scores. Each box is one cell of the array; the address below each
box is the byte address where that cell starts. Assume the array’s block begins at address
1000 and each number takes 4 bytes, so the cells start at 1000, 1004, 1008, 1012, 1016.
1
let scores: number[] =
2
[90, 80, 70, 60, 50];
Why this works
Why does the array need every element to be the same size? If elements could be different sizes, the cells could not be uniform, and you would have no way to know where one element ends and the next begins without inspecting them all. By forcing one type — and therefore one fixed size — the language guarantees the cells form a perfectly regular grid. That regularity is exactly what lets a program jump straight to any element. A mixed-size sequence is possible, but it is no longer this simple back-to-back grid; it needs extra bookkeeping. The plain array trades flexibility for a clean, predictable shape.
Common mistake
A common misconception is that an array’s elements are stored “somewhere in memory” with the array name acting as a list of separate locations. That is not the array model. The elements occupy one block of consecutive addresses. The array name refers to that block — specifically to where it begins. The elements are not scattered; they are a single contiguous run.
An array has 6 elements, and each element takes 4 bytes. How many bytes does the whole array block occupy?
An array of 8 elements occupies a contiguous block of 16 bytes total. How many bytes is each element (the element size)?
An array's block begins at address 1000 and each element is 4 bytes. At what address does element 1 (the second element) start?
In a plain array, every element must have the same type so that every cell is the same size. If an array stores numbers, can one cell store a number and another cell store a 1-character string of a different byte size? Type 1 for yes, 0 for no.
An array of 5 numbers, each 4 bytes, starts at address 2000. At what address does the last element (element 4) start?
What is the memory shape of an array?
An array is a sequence of values of the same type, stored in memory as a run of equal-size cells placed directly back-to-back. Because every element shares one type, every element needs the same number of bytes — the element size — so every cell is identical in size. Because the cells sit with no gaps between them, the array occupies one unbroken block of addresses; this is contiguous memory. An array of N elements, each of size S bytes, is therefore one solid block of N × S bytes. Each value in the array is an element, and the array name refers to the start of the block. This tidy, gapless grid of identical cells is the foundation of everything arrays can do — starting with the instant element lookup you will see in the next lesson.