Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Finding the address of an array element
« on: March 14, 2003, 09:23:43 AM »
Multidimensional arrays can be pretty expensive to lookup, as is clear from the replies already here. To summarise, you basically end getting N-1 multiplications and N-1 additions determining the pointer position for an N-dimensional array.
A compiler usually arranges statically defined multidimensional data in a contiguous block (in first index major order). When it comes to dynamically allocated data (which is usually the case), you have to handle the lookup yourself.

In any event, you may consider redesigning your data arrangement, or access to the critical areas to it, to require fewer dimensions (if possible).

int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Finding the address of an array element
« Reply #1 on: March 14, 2003, 03:20:22 PM »
One way of creating fast multidimensional lookups (ie without multiplication involved) is to create a table of pointers to pointers. This requires more memory (to hold the pointer table) but then you can get a simple lookup technique.

For example, a dynamic 2D array can be set up as follows

/* create our contiguous block memory for the objects */

size_t i;
size_t numRows = ....;
size_t numCols = ....;

Type* data = 0;
Type** rowPointer = 0;

Type* data = (Type*)malloc(sizeof(Type)*numRows*numCols);

/* create our row lookup */
Type** rowPointer = (Type**)malloc(sizeof(Type*)*numRows);

/* Fill the row lookup table */
for (i=0; i{
    rowPointer = &data[i*numCols];
}

/* Now, the rowPointer table contains the offsets to the start of each 'row' in our main data array, so we can access by lookup without multiplication */

Type* p = (rowPointer[rowIndex])[columnIndex];

The principal drawback of this is that you need to allocate space for the rowPointer table. This is usually a lot smaller than the main data however (each entry is just 4 bytes typically), so you might not worry about this.

int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Finding the address of an array element
« Reply #2 on: March 14, 2003, 04:17:58 PM »
@AgaFaster

That's fine for statically sized information, but when you have a dynamically allocated multidimensional array of objects (any kind) where each dimension is initialised with a runtime variable you need to get dirty with pointers.

Also, look at your ptr_curr_z expressions etc. These still involve N-1 multiplications per access which can be a serious overhead in critical areas.
int p; // A