Re: array operations on non-conforming arrays -- do compilers catch them?



On Mar 30, 7:23 pm, "Lane Straatman" <inva...@xxxxxxxxxxx> wrote:
"Beliavsky" <beliav...@xxxxxxx> wrote in message

news:1175288168.015467.93880@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>I am finding that most Fortran 95 compilers do not catch, even at run
time, many invalid uses of non-conforming arrays when array subscripts
are variables. For the following invalid code,

program xsum_nonconform
integer, parameter :: n = 3
integer :: i,ivec(n)
ivec = 1
do i=n,n
print*,ivec(1:n-1) + ivec(1:i)
end do
end program xsum_nonconform

Is it your intent to have the do loop go from n to n?

Yes. For the simpler but equivalent program

program xsum_nonconform_simple
integer, parameter :: n = 3
integer :: i,ivec(n)
ivec = 1
i = n
print*,ivec(1:n-1) + ivec(1:i)
end program xsum_nonconform_simple

the compilers behave the same way, but simplifying further by plugging
in n for i in the print statement to give

program xxnonconform
integer, parameter :: n = 3
integer :: ivec(n)
ivec = 1
print*,ivec(1:n-1) + ivec(1:n)
end program xxnonconform

gives a compile-time error for gfortran and g95 (the only two I
tried), with gfortran saying

xxnonconform.f90:5.7-25:

print*,ivec(1:n-1) + ivec(1:n)
1 2
Error: Shapes for operands at (1) and (2) are not conformable

.