Re: Double precision arrays and subroutines
- From: "Googleer" <stocksami@xxxxxxxxxxxxx>
- Date: 12 Apr 2007 13:52:39 -0700
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.
.
- Follow-Ups:
- Re: Double precision arrays and subroutines
- From: subspace
- Re: Double precision arrays and subroutines
- From: glen herrmannsfeldt
- Re: Double precision arrays and subroutines
- References:
- Double precision arrays and subroutines
- From: Shug Boabby
- Re: Double precision arrays and subroutines
- From: Brooks Moses
- Double precision arrays and subroutines
- Prev by Date: Re: print formatting of double precision variables
- Next by Date: Re: upgrading
- Previous by thread: Re: Double precision arrays and subroutines
- Next by thread: Re: Double precision arrays and subroutines
- Index(es):
Relevant Pages
|
Loading