Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ptsfa!ames!elroy!cit-vax!tybalt.caltech.edu!walton
From: walton@tybalt.caltech.edu.UUCP
Newsgroups: comp.lang.fortran
Subject: Re: ALLOCATABLE, ARRAY :: A(:)
Message-ID: <3174@cit-vax.Caltech.Edu>
Date: Wed, 8-Jul-87 18:08:58 EDT
Article-I.D.: cit-vax.3174
Posted: Wed Jul  8 18:08:58 1987
Date-Received: Sat, 11-Jul-87 14:46:24 EDT
References: <1215@batcomputer.tn.cornell.edu> <105@anumb.UUCP>
Sender: news@cit-vax.Caltech.Edu
Reply-To: walton@tybalt.caltech.edu.UUCP (Steve Walton)
Organization: Calfornia Institute of Technology
Lines: 55

In article <3515@well.UUCP> rchrd@well.UUCP (Richard Friedman) writes:
>  
>About the issue of ALLOCATE arrays, I suspect that nothing
>will be done to catch use of an ALLOCATE array as an
>argument to a subprogram.  I dont see how the compiler can
>detect it unless it is a global compiler.  It just wont work.
>Its very hard to implement dynamic arrays in FORTRAN.

Yes, I know.  Let me see if I can illustrate the point with
side-by-side FORTRAN and C code.

Normal F77

	real array(10)			float array[10];
	call sub(array)			sub(array);
	     ...			    ...
	subroutine sub(a)		sub(a)
	real a(*)			    float *array;

Fortran-8x, allocated

	real, allocatable:: array(:)	float *array;
	allocate(array(10))		array = malloc(10*sizeof(float));
	call sub(array)			sub(array);
	      [sub is the same as above in both instances]

So far, so good.  What if you want to allocate in the subroutine?

	real, allocatable:: array(:)	float *array;
	call sub(array)			sub(&array);
	      ...			   ...
	subroutine sub(a)		sub(a)
	real, allocatable:: a(:)	    float **a; {
	allocate(a(10))			    *a = malloc(10 * sizeof(int));

Notice that in the C example, we have to pass a pointer to a pointer
to accomplish the within-the-subroutine allocation.  But, the choice
has to be made at compile time, while the Fortran-8x spec seems to
require the choice to be made at run time.  And, if we simply say,
"ALL Fortran-8x arrays will be passed by double-offset (pointer to
pointer)" we break the second example where sub is presumably
expecting a pointer.
      I think the way out is actually in the spec: if you do CALL
SUB(A) you are passing a pointer to a pointer to A.  If you do CALL
SUB(A(5)), you are passing element 5 of a as a scalar, and that is a
call by reference.  To send elements 5 through 10 of A to SUB, you
must do CALL SUB(A(5:10)), in my current reading of the spec.  So, you
can unambiguously tell at compile time whether a scalar or an array is
intended and pass a pointer in the first instance and a
pointer-to-a-pointer in the second instance. 
     Does this sound reasonable?

    Steve Walton, guest as walton@tybalt.caltech.edu
    AMETEK Computer Research Division, ametek!walton@csvax.caltech.edu
"Long signatures are definitely frowned upon"--USENET posting rules