Systems CourseC/C++ · Linux · Memory
C++ Language & SafetyLesson 18 of 441 min read6 questions

Arrays & Memory Contiguity

Contiguous in virtual space; scattered in physical RAM.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. int is 4 bytes → an array of 100,000 ints ≈ 400 KB.
  2. An array MUST be contiguous: arr[i] is computed as base + i*sizeof(int) — pointer arithmetic depends on it.
  3. Contiguity is required in virtual address space, not physical RAM. The array's pages can sit in scattered frames; the page table makes them appear contiguous.
  4. Big arrays belong on the heap (new/vector); a large local array can overflow the ~1–8 MB stack.
  5. With demand paging, virtual space is reserved immediately but physical frames arrive lazily on first touch.

Why scattered-physical is fine

A 400 KB array spans ~100 pages (4 KB each). They're contiguous in VIRTUAL addresses (what arr[i] uses) but can map to scattered physical frames. Within a page the ints are contiguous; across a page boundary the next frame may be anywhere — the virtual addresses stay continuous, so indexing never notices.

Watch out

Easily confused

virtual-contiguous vs physical-scattered
The array is one continuous run of VIRTUAL addresses (what indexing uses). In physical RAM its frames can be scattered — fine, thanks to the page table.
stack array vs heap array
int a[N] local = on the stack (small, risky for big N). new int[N] / std::vector = on the heap (large).

Test yourself

6 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.