Arrays

1 / 8

# Arrays - PowerPoint PPT Presentation

Arrays. Memory organization. Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left. Arrays. A collection of variables, all of the same type. Each variable in an array is accessed by an index.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Arrays' - havyn

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Arrays

Memory organization
• Table at right shows 16 bytes, each consisting of 8 bits
• Each byte has an address, shown in the column to the left
Arrays
• A collection of variables, all of the same type.
• Each variable in an array is accessed by an index.
• An array of size n has indices from 0 to n-1.
• An array occupies a contiguous block of memory.
• The size of an array is fixed, specified at creation.
Accessing an array member
• t type of elements in array
• s size (in bytes) of an element of type t
• b base address of array
• address of element i is

b + i * s

Array access example

In Java, an int occupies 4 bytes:

int [] a = new int[5];

The base address of ‘a’ is 21380002.

Indices are 0, 1, 2, 3 and 4.

Total size needed for array is 20 bytes

(5 cells times 4 bytes per cell)

Array access example

a[0]

Where is a[0]?

b + s * i

where b = 21380002, s = 4 and i = 0:

21380002 + 4 * 0 = 21380002

a[0] occupies bytes 21380002, 21380003, 21380004 and 21380005.

Array access example

Where is a[3]?

b + s * i

where b = 21380002, s = 4 and i = 3:

21380002 + 4 * 3 = 21380014

a[3] occupies bytes 21380014, 21380015, 21380016 and 21380017.

a[3]

Array access example
• Where is a[7]? There is no such array cell, so what happens if we try to access it?
• In some languages (e.g. C and C++) the access is permitted, with unpredictable results.
• Java throws an ArrayIndexOutOfBoundsException