Before we can talk about arrays, we need a working picture of where a program keeps its data. Not a precise hardware diagram — a simplified model that is accurate enough to predict what your code will cost. Almost every claim in this course, including why reading arr[i] takes the same time no matter how large the array is, follows from this one model.
Start with the smallest interesting thing a computer does: adding two numbers. The CPU holds a handful of tiny slots called registers, and an arithmetic unit that can add the values sitting in them. Feed it 10 and 6 and it produces 16.
But registers number in the dozens, not the millions. The moment the CPU needs that 16 again later — or needs to hold a thousand values at once — it has to put the result somewhere else. That somewhere is main memory, or RAM.
Picture RAM as one enormously long row of numbered slots. Each slot holds a fixed, tiny amount of data — one byte — and each has a permanent number called its address. The addresses run in order, with no gaps, from one end of memory to the other.
Free account
Sign up to read the rest of this lesson: 2 more sections.
Still to come