Results 1 to 3 of 3
  1. #1
    hellolleh is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default 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

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

    My Question

    How does the memory representation of an 2 dimensional array look?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    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. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Graphics: Buffered Array as Video Memory
    By TiagoCT in forum Java 2D
    Replies: 2
    Last Post: 11-11-2009, 01:34 PM
  2. Array memory allocation
    By number5isalive in forum Advanced Java
    Replies: 8
    Last Post: 10-14-2009, 08:04 AM
  3. graphical representation of website structure
    By mrpwnt in forum New To Java
    Replies: 0
    Last Post: 12-09-2008, 10:08 PM
  4. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  5. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 11:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts