May 03, 2024, 11:31:45 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Multi-dimensional array problem

Started by sapero, August 14, 2007, 04:55:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

Hello, I have often a dilemma - how it really works.
Having an simple array int x[5] - all works ok, but my brain hungs with [x,y] and
  • [y].

    Sometimes while converting C examples, I need to swap the x with y, so
    int x[2][3] becomes int x[3,2]

    a = x[1][2]  becomes  a = x[2,1]


    I've defined a simple array: float x[3,2], filled it with MoveMemory, and found that it works in this way: [element, dimension]
    I remember also the trick for accesing array of strings: [0, string_index], but (for me) more logical is [dimension, element].

Ionic Wind Support Team

The students in the IW course just learned about this one.  Here is an excerpt from the text:
----------------------------------------------------------
Arrays

An array is a collection of data of the same type. Arrays are referenced by their variable name and one or more index offsets. The index offset is zero based and sized to the type of the base variable.  The size of an array in bytes is equal to the product of the dimensions times the size of the base variable type.

To specify an array definition use the [ ] symbols and enter up to 5 dimensions separated by commas. For example a three dimension array of floats would be defined as:

DEF vectors[5,5,5] as FLOAT

The total memory needed for that array is 5*5*5 * 4 or 500 bytes.  4 is the size of a FLOAT variable in bytes. For each dimension the range of index offsets is 0 to dimension-1 which is an important point to remember.  For our array above the first element of the array would be at index 0,0,0 and the last element of the array would be at index 4,4,4.

The compiler does not do bounds checking on your index values so it is possible to read and write past the end of the array causing possible memory corruption.

Array are stored in memory in column major order.  Save the following test program as unit3_4.eba and run it for an illustration of array memory usage and element ordering.


'------------------------------------------
'John Doe
'100-20
'April 10 2007
'Program to show array memory usage.
'unit3_4.eba
'Revision 0
'------------------------------------------
'set up a three dimensioned array to contain the values
'0 1 2
'3 4 5
'6 7 8

DEF array[3,3] as CHAR
DEF p as POINTER
x = 0
FOR ROW = 0 to 2
FOR COLUMN = 0 to 2
'show this elements address in memory
print &array[ROW,COLUMN]
array[ROW,COLUMN] = CHR$(x+0x30)
x++
NEXT COLUMN
NEXT ROW
PRINT
'show contents of the array by reading memory directly
p = &array
FOR COUNT = 0 to (3*3*1)-1
PRINT *<CHAR>(p + COUNT)," ",
NEXT COUNT
PRINT
PRINT:PRINT "Press any key to continue"
DO:UNTIL INKEY$ <> ""
END


What this shows us is that the column, or right most index, has the most significance in the calculation the compiler uses to address an array element. In general the only time you will need to know this is when sending multiple dimensioned arrays to 3rd party DLL's that might expect a different ordering in memory. For example C expects arrays to be in row major while FORTRAN and MATLAB expect arrays to be in column major format.

As to why Emergence uses column major ordering? It was a personal preference of mine and common in BASIC languages of the past.
Ionic Wind Support Team

sapero

Thank you. I have understand
Here is my example, it displays same order in emergence and aurora, and better shows the memory organization for arrays

float f[3,2] :' 2 times float[3]

' temporary 1-dim array
float load[3*2]

load = /*dimension 0*/0.0, 0.1, 0.2, _
       /*dimension 1*/1.0, 1.1, 1.2

' copy array 'load' to 2-dimensional array 'f'
declare extern _memcpy(...)
const SIZEOF_FLOAT = 4
_memcpy(&f, &load, 3*2*SIZEOF_FLOAT)

setprecision 1
print f[0,0], f[1,0], f[2,0] /* dimension 0 */
print f[0,1], f[1,1], f[2,1] /* dimension 1 */


BTW. forum's [ code ] block eats newline characters while copying the text, and the php script has problem with [ x ] [ x ] without spaces (my first post)