Course contentsShow
← Computer Science
Lesson 237 of 2,8726. Arrays and Basic CollectionsFree lesson

Array Memory Layout

Understanding how arrays are stored contiguously in memory and calculating element addresses.

Array Memory Layout

What you'll learn: You'll understand how arrays are physically stored in memory and how computers quickly find any element using simple math.

Why Arrays Are Fast

When you declare an array, the computer reserves a contiguous block of memory—meaning all elements sit side-by-side in a single unbroken line, like houses on a street with consecutive addresses. This is different from scattered storage where elements could be anywhere.

How Memory Addresses Work

Imagine memory as a numbered street where each "house" (memory location) has an address. If you declare int numbers[5], the computer:

  1. Finds space for 5 integers in a row
  2. Records the base address (the starting location)
  3. Knows each int takes a fixed amount of space (say, 4 bytes)

Calculating Element Positions

To access numbers[3], the computer doesn't search through the array. Instead, it calculates:

Address of numbers[3] = base_address + (3 × size_of_int)

If the base address is 1000 and each integer is 4 bytes:

  • numbers[0] → address 1000
  • numbers[1] → address 1004
  • numbers[2] → address 1008
  • numbers[3] → address 1012

One multiplication and one addition—instant access! This is why array indexing is so fast and why it's called "random access."

The Memory Layout Guarantee

Because arrays are contiguous, iterating through them is also fast. The computer can predict where the next element lives, making efficient use of memory caching.

Key Takeaway: Arrays store elements in consecutive memory locations, allowing instant access to any element through a simple calculation: base_address + (index × element_size).