I guess anyone with a little bit of programming experience knows the storage of multidimensional arrays. For simplicity, I will just deal with 2D arrays.
In C,C++,etc. memory storage is row-major ordered, meaning that rows are stored consecutively. A 3X4 grid would be stored in the memory as follows:
1 2 3 4
5 6 7 8
9 10 11 12
In Fortran or Matlab, that would be:
1 5 9
2 6 10
3 7 11
4 8 12
So now, my old knowledge from analytical geometry forces me to think about the following axis system:
y
|
|
|
|
|
---------------- x
That certainly doesn't fit to the row-major storage system. I have seen so many people with math background that would access the ith row and the jth column like A[j][i] in C++. Why? Because that is the geometric way to do it. By moving along the x-axis, you change the current column and the x-axis naturally seems to be the first axis to be written in row-major storage.
A[x-axis value][y-axis value]
But that's plain wrong, in fact all accesses are of the form A[y-axis value][x-axis value]. If you want to increase the current column you're in, you change the second dimension like A[i][j++]
Weird, right?
Subscribe to:
Post Comments (Atom)
I suppose people with some kind of math background would access to an array elements the way below:
ReplyDeleteA[index(i, j)];
where 'i' is an x-axis coordinate, 'j' is a y-axis coordinate, 'A[]' is an unidimensional n*n array, 'index()' can be defined like this:
#define index(x, y) (x * n + y)
Neat idea !
ReplyDeleteThat would be convenient for them but possibly a pain for anyone trying to decipher their code.