Re: some confusing in subroutine

From: David Ham (D.A.Ham_at_CiTG.TUDelft.nl)
Date: 10/27/03


Date: 27 Oct 2003 15:30:45 +0100


"Zhang yunfeng" <zyf_sz@hit.edu.cn> writes:

> hi, all.
> here's a subroutine, although "goto" is used, but the structure is clear.
> what i confused is the array "A,B,C" is declared only one element. why in
> the next, the array A,B,C is used like they has more than one element?
> it 's said that the code is written in FORTRAN IV. and could compiled with
> NDP-FORTRAN. is the code correct in Fortran IV?
> appreciate for your advice
>
>
> SUBROUTINE SHTADV (A,B,C,AA,NN,IIND)
> IMPLICIT REAL*8 (A-H,O-Z)
> DIMENSION A(1),B(1),C(1)
> GO TO (5,15,25),IIND
>
> 5 DO 10 I=1,NN
> 10 A(I)=B(I) - C(I)
> RETURN
>
> 15 DO 20 I=1,NN
> 20 A(I)=B(I) + C(I)
> RETURN
>
> 25 DO 30 I=1,NN
> 30 A(I)=A(I) - B(I)*C(I)*AA
> RETURN
> END

This is an ancient and truly hairy kludge to get around the fact that
pre-f77 dialects of Fortran did not allow for variable sized
arguments. The idea, I believe, is that you hope and pray that the
compiler generates a program which passes A by passing the location in
memory of the first entry in A. This is how C does arrays. It's not
remotely legal in any Fortran standard after 1977 (since I was one in
1977 I'll defer to the more ancient denizens of clf as to whether it
was ever legal) and it tends to cause modern compilers with even
primitive bound checking to barf at compile time.

There are (at least) three ways of fixing this:

1. Find a way to switch off bounds checking on your compiler. This is
the easiest if the program is big but it's obviously not portable.

2. Replace the declarations with assumed size arrays:
DIMENSION A(*),B(*),C(*)

This makes the declaration legal f77 (and f90/f95).

3. Replace the declarations with assumed shape arrays:
DIMENSION A(:),B(:),C(:)

This makes the definition legal f90/f95 (but not f77).

Regards,

David



Relevant Pages

  • Re: Double precision arrays and subroutines
    ... arrays. ... What happens is that, in Fortran, these two program units are ... The subroutine and the program that calls it are compiled ... and normally the compiler doesn't know about one when it's ...
    (comp.lang.fortran)
  • Re: The Decline of C/C++, the rise of X
    ... > meta-programming challenging Fortran in numerical performance, ... they must be conformant (arrays of the ... that places heavy demands on a compiler. ... and have a syntax error, the resulting error messages is often ...
    (comp.programming)
  • Re: Who uses clapack?
    ... Fortran 95 still has many advantages over C and C++: ... the ability to pass one- and multi-dimensional arrays to functions ... >Have you ever seen those bills for a Fortran compiler for an ... Lapack documentation is at http://www.netlib.org/lapack/lug/index.html ...
    (sci.math.num-analysis)
  • Re: Language efficiency of C versus FORTRAN et al
    ... > that a FORTRAN compiler can. ... > not least because I don't understand FORTRAN. ... when you pass arrays to "subprograms". ... If C or Fortran programmers do this, ...
    (comp.lang.c)
  • Re: confused with a g77 error message
    ... Those integer statements are not valid Fortran of any standard ... some compiler somewhere that compiled this stuff, ... The "if" statements are executable statements. ... follow all the declarations. ...
    (comp.lang.fortran)

Loading