row_major.c:
#include <stdio.h>
int main() {
char a[4][3][2];
int i;
for (i = 0; i < 4; ++i) {
int j;
for (j = 0; j < 3; ++j) {
int k;
for (k = 0; k < 2; ++k) {
printf("(%d, %d, %d) ==> %d\n", i, j, k,
(int)(&a[i][j][k] - &a[0][0][0]));
}
}
}
return 0;
}
$ gcc -Wall row_major.c
$ ./a.out
(0, 0, 0) ==> 0
(0, 0, 1) ==> 1
(0, 1, 0) ==> 2
(0, 1, 1) ==> 3
(0, 2, 0) ==> 4
(0, 2, 1) ==> 5
(1, 0, 0) ==> 6
(1, 0, 1) ==> 7
(1, 1, 0) ==> 8
(1, 1, 1) ==> 9
(1, 2, 0) ==> 10
(1, 2, 1) ==> 11
(2, 0, 0) ==> 12
(2, 0, 1) ==> 13
(2, 1, 0) ==> 14
(2, 1, 1) ==> 15
(2, 2, 0) ==> 16
(2, 2, 1) ==> 17
(3, 0, 0) ==> 18
(3, 0, 1) ==> 19
(3, 1, 0) ==> 20
(3, 1, 1) ==> 21
(3, 2, 0) ==> 22
(3, 2, 1) ==> 23
This matches what the FFTW3 manual describes: For an n_1 x ... x n_d array, (i_1, ... i_d) ==> i_d + n_d * (i_{d-1} + n_{d-1} * (... n_2 * i_1) ... ). But then i_d = the "row coordinate" since it is runs over this index that are grouped together? Having a[i][j] refer to the ith column and jth row will take some getting used to although it is another thing I should've been aware of long ago. (On the other hand, I rarely use staticly allocated multidimensional arrays and so have almost certainly never written anything incorrect as a result of this gap in knowledge.)
//Does Java use the same convention? How about the various packages for allocating arrays in C?
No comments:
Post a Comment