On Wednesday, December 13, 2017 at 10:39:31 AM UTC-8, pehache wrote:
(snip, I wrote)
> >> > Well, C does have something that allocates the same as multiple
> >> > dimension arrays.
> >> I would rather say "...that simulates multiple dimension arrays."
> > If you say:
> > int x[10][10];
> > It allocates 100 elements in contiguous storage, and if
> > referenced, the compiler does the usual multiply and
> > add to find an element.
> OK, but under the hood this is still pretty different from Fortran. The two
> subscripts here are not equivalent: as far as I know (but maybe I'm wrong
> ?) x is still an address in this case (the address of x[0]). So I
> guess that the compiler implicitely creates first a 1d array of pointers.
As far as I know, both are implemented the same way.
Actually, neither Fortran nor C has a requirement on how they
are implemented, as long as they do what they are supposed to do.
I remember the PDP-11 Fortran compiler generated an array of
addresses of rows, as that is faster than multiply.
I would say the main difference is that in C you can say:
float x[10][10], y[];
y=x[i];
y[j]=5;
which you couldn't do in Fortran before pointers, and still
isn't quite as easy.
For most compilers, i will be multiplied by 40, and added to
the origin of x, and assigned to y. Then 4*j is added, and
used to address y[j] to assign 5 to it.
But the thing that is strange about C is that the same
syntax has different meaning, depending on the declaration.
If you say:
float **z, *y;
you can also reference it as z[i][j], and can also
y=z[i];
y[j]=5;
but in this case, z is an array of pointers, and y is a pointer.
> > On the other hand, if in Java you say:
> > int[][] x=new int[10][10];
> > you get an array of 10 object references to 10 arrays
> > of 10 elements each.
> To me it's still the case in C, except that in C the compiler is "forced"
> to use contiguous memory for the storage in such cases.
For the x[10][10] form, most compilers will find the array
element by multiplying i by 40, but, as with Fortran, an array
of pointers that you can't assign to is also a possible implementation.
> >> > It does, however, reference them as arrays
> >> > of arrays. When it does that, the subscripts are (normally) in
> >> > the order of row major.
(snip)
> > As I noted, it is only the desire for source to be read left
> > to right that causes that. If [] was right associative, then it would
> > be column major, but you would write it [j]x which looks strange.
> It looks strange, it beats the maths convention for the matrix notations,
> and most of all it's a different syntax. In contrast, as I wrote earlier,
> choosing a different storage convention in Fortran would have had no impact
> on the syntax.
> > But as I noted earlier, since [] is commutative:
> > x[j] == (i[x])[j] == j[i[x]], the latter, with j before i,
> > could be considered column major. But again, it looks strange.
> > And yes, the C language requires this to work.
> Really ? ? I didn't know that (but I'm not a skilled C programmer)
x[i] is defined to be the same as *(x+i).
Since addition is comutative, even in this case, it equals *(i+x),
which is, by the same definition, i[x].
The same transformation works for higher dimensions.