Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!hc!lanl!beta!jlg
From: jlg@beta.lanl.gov (Jim Giles)
Newsgroups: comp.lang.fortran
Subject: Re: Should I convert FORTRAN code to C?
Message-ID: <20531@beta.lanl.gov>
Date: 3 Jul 88 22:08:31 GMT
References: <2742@utastro.UUCP> <20008@beta.UUCP> <224@raunvis.UUCP> <12292@mimsy.UUCP>
Organization: Los Alamos National Laboratory
Lines: 58

In article <12292@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes:
> In article <20518@beta.lanl.gov> jlg@beta.lanl.gov (Jim Giles) writes:
> >You haven't been reading this discussion at all!  The C gurus all have been
> >saying that a static multidimensional array is implemented as pointer to
> >array, but that a dynamic multidimensional array is implemented as pointer
> >to pointer (to pointer ... etc for the number of dimensions).
> 
> Only the false gurus.  :-)

I would like to see dynamically allocated multi-dimensional arrays in
C that aren't declared as:
    int **a ...
But all implementaions (both here on the net and in journals) have done
this.  The result is that a reference to such an array really IS turned
into pointer-to-pointer type constructs.  If you have an alternate
implementation, please send it to me and I'll shut-up.

> [...]
> Ideally, one should be able to say
> 
> 	void foo(int m, int n, int arr[n][m]) {
> 		... /* ref arr[j][i] */ ...
> 	}
> 	/* called as foo(M, N, arr) */
> 
I agree.  The point is that you can't.  Furthermore, 'foo' has to
be implemented twice if the expected argument (arr) could be either
statically or dynamically allocated.  In the later case, the declaration
has to be: int **arr.

> [...]
> >... The Fortran generally runs faster because its
> >conception of array is better suited to these types of optimization.
> 
> which is correct, but not because of the form of subscripting, but
> rather because of an implicit rule against aliasing---a rule that I
> have seen violated in FORTRAN programs, incidentally.  (Such programs
> are allowed to quietly produce mysterious results [as are malformed C
> programs that are equally easy to come by: different error, same lack
> of obviousness].  Ever hear someone gripe `but it works on VAX/VMS; the
>  compiler must be broken'?)

Which is what I pointed out when I said that C needs the 'noalias'
directive.  The Fortran rule against aliasing is not implicit either.
The standard specifically prohibits it.  Due to separate compilation
constraints, the compiler can't check the problem.  But the prohibition
is explicit.

By the way, the Fortran 8x version of dynamic memory allocation doesn't
have the pointer-to-pointer problem.  When you declare an allocatable
array, its dimensionality is part of the declaration.  The ALLOCATE
statement then takes the actual dimensions and dynamically allocates
the whole space.  From a subroutine's point of view, all arguments
which are declared multi-dimensional are treated the same whether
they were dynamically allocated or not.

J. Giles
Los Alamos