# Thread: Memory representation of an array?

1. Member
Join Date
Feb 2010
Posts
5
Rep Power
0

## Memory representation of an array?

We can view memory itself as a giant array. On moden computers, memory is implemented in hardware as a sequence of indexed memory locations that each can be quickly accessed with an appropriate index.

Suppose that a computers memory is organized as 1000 values, with addresses from 000 to 999. Suppose that an array of eight elements is stored in memory locations 523 through 530. In such a situation, Java would store the memory addresses of the first array value somewhere else in memory, along with array length. We refer to the adress as a pointer and think of it as pointing to a location.

It would look something like this

000
...
123 523 //array location
124 8 //length of array
...
523 array[0]
524 array[1]
...
530 array[7]
...
999

My Question

How does the memory representation of an 2 dimensional array look?
(Array[][])

2. Interesting. I think classically (in C) multi dimensional arrays could also be interpreted as multi dimensional pointers. I'm not entirely sure how Java Virtual machine would do it, but I would expect the outermost dimension would be as you said, a continuous region of memory. Only its values, instead of being a data type value, would in turn be a pointer, to the contents of the array at that location, which in this case. and while the contents of the second dimensional array, say the first index on the outer dimension, e.g. Array[0][],, would itself also be a continous organisation of memory addresses of the length of its array, there would likely be no guarantee that this would sequentially follow the continous memory address of the outer array.

that is, we probably could not just find the first element of Array[0][0] at memory address 531 (the address after the single outer array in your example(

3. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
8
I hope this isn't considered necroing, if it is, I appologise in advance.
The deal with 2d arrays, is that they are acctually arrays of arrays. If you look at how a 2d array is initialized, this becomes quite clear. You define an array by specifiying the type that it holds (let's say int) and add brackets to declare it's an array, not a single variable. So if you write int[][] arr; you can look at it as an array, that holds int[] elements, rather than a 2d array of ints.

So let's say we have an array of Objects:
Java Code:
`Object[] arr = new Object[10];`
So what we do then, is fill the Object array with Object arrays:
Java Code:
```for(int i = 0; i < 10; i++)
arr[i] = new Object[10];```
This means, that the array arr stores the references to the newly created arrays. So, in essence, the first array would occupy the memory spaces from 300 to 309, and each of those spaces would point to another memory address, where the acctual array is held.