Re: Double precision arrays and subroutines



On Apr 12, 12:40 pm, Brooks Moses <bmoses-nos...@xxxxxxxxxxxxxxxxxx>
wrote:
Shug Boabby wrote:
After some processing, it then calls a subroutine

call av (nx, workd(ipntr(1)), workd(ipntr(2)))

I looked at the definition for av, expecting it to require an integer
and two doubles. However, the first few lines of av are

subroutine av (nx, v, w)
integer nx
Double precision v(nx*nx), w(nx*nx)

which I believe means that it is expecting an integer and two double
arrays.

My only explanation would be that workd(ipntr(1)) is sending a pointer
to this element in the array, and then av is considering that pointer
to be the start of an nx * nx array.

Clarifications would be greatly appreciated.

To add to what other people have said: Yes, that's essentially what's
happening, but it's not legal Fortran code.

What happens is that, in Fortran, these two program units are
independent. The subroutine and the program that calls it are compiled
separately, and normally the compiler doesn't know about one when it's
compiling the other. Although they're usually in the same source file,
it's useful to imagine that they're in separate files being compiled at
separate times and then linked together.

Thus, the compiler doesn't know about the mismatch between the fact that
the subroutine call is being given an integer and two scalar doubles,
and the subroutine declaration is expecting an integer and two double
arrays. It compiles the subroutine call to pass along scalar doubles,
and it compiles the subroutine to expect double arrays.

This sort of thing is, in most cases, called a "bug".

In this case, however, it seems that the way that this Fortran compiler
works is that on the machine-code level it doesn't actually pass the
values; it passes their location in memory -- that is, a "pointer" in
the C-language sense of the word. And so, for that particular compiler,
this piece of code happens to work right.

Other compilers do things like passing the size of an array along with
its location in memory, and thus on those compilers this code will fail
in ugly and difficult-to-debug ways. But apparently this program was
never (we hope!) run on such a compiler....

- Brooks

P.S. Note that, in more recent versions of Fortran, there are much
better ways to write this code -- both to allow the compiler to check
and make sure the subroutine does match the subroutine call, and to pass
sections of arrays to subroutines. Please don't judge modern Fortran by
this historical ugliness! :)

--
The "bmoses-nospam" address is valid; no unmunging needed.


This code might be ugly, by modern standards, but it isn't illegal.
It's they way things were commonly done in Fortran77. As long as the
user keeps track of things then this code will work fine.

Fortran has always passed what would be called pointers in C.


.



Relevant Pages

  • Re: f95 OOP question
    ... and I managed to bring the intel compiler to its knees. ... until you understand Fortran 95 a little better. ... The term module means the same thing in Fortran 95 that it does in every other computer programming language. ...
    (comp.lang.fortran)
  • Bug in IBM Fortran compiler?
    ... while trying to compile an application with the IBM Fortran compiler I ... "Add" subroutine, but I just found that the assignment ...
    (comp.lang.fortran)
  • Re: seeking advice for "translate" Fortran code to Java code
    ... redefine a common block as needed for each subroutine. ... Still, Java has no pointers, so I ... Java arrays are always what C would call arrays of pointers ... Fortran intrinsics in Java appropriately. ...
    (comp.lang.fortran)
  • Re: beginners question
    ... If you pass a literal to a subroutine, the compiler puts the ... literal into a temporary variable and passes a reference to that ... Also, since FORTRAN was developed at a time when memory was very tight, ...
    (comp.lang.perl.misc)
  • Re: Double precision arrays and subroutines
    ... to be the start of an nx * nx array. ... What happens is that, in Fortran, these two program units are independent. ... The subroutine and the program that calls it are compiled separately, and normally the compiler doesn't know about one when it's compiling the other. ... Thus, the compiler doesn't know about the mismatch between the fact that the subroutine call is being given an integer and two scalar doubles, and the subroutine declaration is expecting an integer and two double arrays. ...
    (comp.lang.fortran)

Loading