Tom Clune writes:
> I have a question about what the F77/F90 standards say about making
> out of bounds references in arrays.
That much is (relatively) easy. They say you can't. F90 6.2.2.1:
"The value of a subscript in an array element must be within the
bounds for that dimension."
> I have looked at them, but my problem seems to fall in a gray area....
[example elided]
> It is true that the listed lines WOULD reference outside the bounds of
> z's third dimension, IF those lines were executed. The if test
> explicitly prevents this by checking if 'nd' is 10 or not. So
> logically and in execution, there is no problem.
Umm. A bit subtle. The lines would be illegal if you ever executed
them, but you don't. The question is not whether it is legal to make
an out-of-bounds reference (that's not legal), but whether this even
*is* a reference until it is executed.
I might go so far as to say that the question relates to what the
definition of "is" is. :-)
The above citation appears to be the pertinent part of the standard,
but it's a bit tricky to interpret here. In general, a subscript is
an expression, the value of which may not be known until it is
evaluated at run-time. So the cited restriction pretty much has to be
a run-time restriction.
I conclude that the code is technically standard-conforming....I
think. But its subtle. I was hoping that someone else would
answer this first so that I wouldn't have to post an answer that
I had this much uncertainly about...I'm now waiting for someone to
show that I'm wrong.
You didn't ask for opinions on the style, so I won't offer any. But
I'll bet there is more than one compiler that will reject it. The
compiler probably figures it is being "helpful" in rejecting it at
compile time instead of letting the "obvious bug" hide until run-time.
I will note that there was a vaguely similar interpretation question
about whether it was legal to do things like
...
real, intent(in) :: x
...
if (.false.) x = 1.0
That one got resolved by adding syntax constraints against X even
appearing in a context like that, regardless of whether or not it
actually got executed. But that solution was specific to intent(in);
it doesn't generalize to other things that would be illegal if they
got executed.
--
Richard Maine
[log in to unmask]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|