algoritmauzmanı
Sign in
TREN
2Arrays
Lesson 2.0

What is an array?

An is the most basic : it keeps elements of the same type side by side in memory. Many structures, such as stacks, queues, heaps and hash tables, are built on top of an array. This lesson explains how an array sits in memory, how the place of an element is computed and where the limits of an array lie.

What you will learn
  • That an array sits in and why the starts at 0
  • How the address of an element is computed and why access is
  • The fixed size of an array and the difference between and size
6 min

1Cells side by side

When you declare an array, a single block of memory is reserved. The block is split into cells of equal size and each cell holds one element. There are no gaps between the cells: the second cell starts where the first one ends.

0123120x10070x10430x10890x10c
An int array of four elements. Indexes above, below. Each int takes 4 bytes, so the addresses go up by 4.

The position number of a cell is its . In C, Java and Python the index starts at 0: the first element is arr[0] and the fourth is arr[3]. In an array of n elements, the last index is n - 1.

2The address formula and O(1) access

The index starts at 0 because of the address formula. The index says how many cells the element is away from the start of the array. The address of an element is found like this:

address(arr[i]) = start address + i × element size

In the array above, the start address is 0x100, which is 256, and each int takes 4 bytes. The address of arr[3] is 256 + 3 × 4 = 268, which is 0x10c. The first element is 0 cells away, so its address is the start address itself.

ElementFormulaAddress
arr[0]0x100 + 0 × 40x100
arr[1]0x100 + 1 × 40x104
arr[3]0x100 + 3 × 40x10c
arr[1000]0x100 + 1000 × 40x10a0

The formula is one multiplication and one addition; it takes the same time for element 3 as for element 1000. That is why access by index is . A linked list has no such formula: you walk from the head to reach node k.

3The size is fixed up front

The block of an array is reserved once and does not grow afterwards. The memory right after it may belong to another variable, so the array cannot spread into it. If more room is needed, a new, larger block is reserved and every element is copied over. A structure that does this by itself is called a dynamic array.

C does not check whether an index stays inside the array. arr[4] reads memory outside a four-element array, and the program may carry on with a wrong value and no error. Java throws ArrayIndexOutOfBoundsException and Python raises IndexError.

4Declaring arrays in C, Java and Python

In C, the type and the number of cells are written in the declaration. If the declaration is inside a function, the cells hold random values.

int grades[5];                 // 5 cells; contents undefinedint primes[4] = {2, 3, 5, 7};  // filled when declaredgrades[0] = 90;                // write by indexprintf("%d\n", primes[2]);     // 5

In Java an array is created with new and its cells are filled with 0. The number of cells is in the length field.

int[] grades = new int[5];     // 5 cells, all 0int[] primes = {2, 3, 5, 7};grades[0] = 90;System.out.println(primes.length);   // 4

Python has no fixed-size array; a list is a dynamic array that grows when needed. In this section, to see how an array works, we create the list with [None] * capacity and keep track of its size ourselves.

grades = [None] * 5    # 5 cells, all Noneprimes = [2, 3, 5, 7]grades[0] = 90print(len(primes))     # 4

5Capacity and size

An array has two numbers. The is the number of cells reserved, and it does not change. The size is the number of cells filled right now, and it changes with every insertion and deletion. An array does not know its own size; the size is kept in a separate variable.

1207132345673size
Capacity 8, size 3. The filled cells run from 0 to size - 1; the first empty cell has index size.

The filled cells always start at the front and leave no gaps. Thanks to this rule, the first empty cell is at size, and the array is full when size == capacity. Every array operation keeps this rule: inserting in the middle shifts elements right, deleting shifts them left.

6What this section covers

Each array operation also has a linked-list version in a lesson of its own with the same name: creating a linked list, traversing, inserting at the head, at the end and at a position. Reading the two side by side shows which structure is good at which job. The comparison is summed up in Introduction to linked lists.

7Try it

In an int array that starts at address 0x200 (each int is 4 bytes), what is the address of arr[5]?

Show the answer

The answer is B. Address = 0x200 + 5 × 4 = 0x200 + 20. 20 in hexadecimal is 0x14, so the result is 0x214. The index counts cells, not bytes, so it is multiplied by the element size.

Four elements have been written into an array with capacity 10. What is the size, and at which index does the next element go?

Show the answer

The answer is B. The size is the number of filled cells: 4. The filled cells are 0, 1, 2 and 3, so the first empty cell is arr[4]: the next index is the size itself.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.