Welcome, Guest. Please login or register.

Author Topic: Finding the address of an array element  (Read 8790 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Finding the address of an array element
« on: March 14, 2003, 01:32:20 AM »
Hello all,

I am attempting to devise a formula to compute the memory address of a multi-dimensional array of arbitrary dimensions and size. So far the solution has been eluding me and I thought maybe you all could help point me in the right direction. Here's the problem:

I have a function that reserves a block of memory that is used to hold values in an array-like method. The function takes sizes of an arbitrary number of dimensions and reserves space to hold as many 4 byte integers. The trouble I'm having is finding the memory location of a specific element. I have formulas for finding the memory location of a 1-dimensional array element and a 2-dimensional array element, but I need a generic formula to find the address of an n-dimensional array element.

1-dimensional:

int base
  • ;

address = base[index1];

address = base + (index * sizeof(int));

2-dimensional:

int base
  • [y];

address = base[index1][index2];

address = base + (index1 * x * sizeof(int)) + (index2 * sizeof(int))

n-dimensional:

int base
  • [y][z][w]...;

address = base[index1][index2][index3][index4]...;

address = ???

This problem must be discussed somewhere because all C compilers use it to convert array indexes into pointer arithmetic when dealing with arrays. Does anyone have any ideas?

Thanks,
Sidewinder
 

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: Finding the address of an array element
« Reply #1 on: March 14, 2003, 02:15:51 AM »
@Blitter

Thanks for the prompt reply.  The approach used in that example is different than the one I am using.  I have one large block of contigious memory large enough to hold all the elements of the array.  This example fragments the array by rows.  Also, the example code deals only with 2 dimensional arrays.  

I need a more generic example or formula for 3, 4, 5,..., n dimensional arrays in a contigious block of memory.  :-(

I'm getting a headache.
Sidewinder
 

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: Finding the address of an array element
« Reply #2 on: March 14, 2003, 06:47:38 PM »
Thank you for all of your help.  I used the offset approach that jdiffend advised and was able to implement a loop to compute the offset and then add it to the base address.  Simply using a bunch of 2D arrays or somthing was not an option because I'm designing a programming langauge and do not wish to impose silly limits on such things when a little time and  thinking could offer a more flexable solution.  Thanks to everyone who helped.  
Sidewinder
 

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: Finding the address of an array element
« Reply #3 on: March 15, 2003, 01:39:08 AM »
@ everybody

So as not to be contrary here is the routine that I came up with.  No problems yet, but only time will tell.    ;-)

/* number_of_dimensions is an int that holds the number of dimensions of the array (clever eh?), i is the standard counter variable.  address holds the memory location.  The element_values[] array is number_of_dimensions in size and holds the desired position of the element that we wish to address : i.e. (2,4,1,5) .  The max_element_size[] array is also number_of_dimensions in size but it holds the maximum values of each of the dimensions: i.e. (10,10,10,10).

i = number_of_dimensions;
place_value = 1;
address = element_values;
i, element_values, address);
i--;

while (i >= 0)               /* While there are more dimensions to calculate. */
{
   place_value *= max_element_size[i + 1];

   address += element_values * place_value;
   
   i--;
}

address *= type_size;
address += base;

And that's about it.

Cheers.
Sidewinder
 

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: Finding the address of an array element
« Reply #4 on: March 16, 2003, 04:37:01 AM »
@ Fluffy

Great idea.  I hadn't considered that.  I think I'll give it a try straight away/
Sidewinder