Anton Shterenlikht <
me...@mech-aslap33.men.bris.ac.uk> wrote:
> It seems I fundamentally mis-understood
> the array bounds checking issue.
> If array bounds checking is requested
> from the compiler, what should the compler
> do when it detects the out of bounds reference
> happening: issue a warning or an error?
> For example, this code:
> integer :: i, a(10)
> do i=1,size(a)+1
> a(i) = 1
> end do
> write (*,*) sum(a)
> end
> will run correctly, i.e. sum(a) is correct,
sum(a), and the contents of a, are likely correct, but
whatever is in memory after a is likely wrong.
In many cases, bounds check can only be done at run time,
but in this case it isn't hard for the compiler to notice.
Cray compilers have a history of optimizing DO loops,
especially on machines with vector registers. It is pretty
easy for the compiler to notice that i goes to 11, and a(11)
is past the end of a.
> as long as there are no bounds checks.
> When the bounds checks are on, gfortran and ifort
> give errors, while crayftn gives a warning and
> the answer:
>
> $ gfortran -fbounds-check z.f90
> $ ./a.out
> At line 4 of file z.f90
> Fortran runtime error: Array reference out of bounds for array 'a',
> upper bound of dimension 1 exceeded (11 > 10)
Reasonably often fetch from outside an array doesn't cause problems,
and issuing a warning and continuing might not be a bad thing.
Storing outside an array is best handled as a fatal error.
In the early days of Fortran (before the assumed size (*) was added)
it was usual to dimension dummy arrays as (1). As long as you stayed
inside the actual array, and no bounds check was done, that worked.
More recently, compilers will detect at least constant subscripts at
compile time, and even, as you note DO loops, that exceed the declared
dimension.
Just fix them!
-- glen