I believe zero-length pointers are allowed in Fortran, like zero-length arrays. When associating such a pointer, does it really matter what the pointed address is? Can it be out of bounds?
Case in point:
program test
implicit none
real, target :: a(100)
real, pointer :: p(:)
integer :: i, ini, fin, n
ini = 1
do i=1,20
n = max(0,min(i,size(a)-ini+1))
fin = ini+n-1
print *,'iter:',i,'elements:',n,'limits:',ini,fin
p(1:n) => a(ini:fin)
ini = fin+1
end do
end program test
The last few iterations create a zero-length pointer, formally pointing out of bounds to a(101:100). When compiling this with -check all I get:
forrtl: severe (408): fort: (2): Subscript #1 of the array A has value 101 which is greater than the upper bound of 100
Image PC Routine Line Source
a.out 0000000000405611 Unknown Unknown Unknown
a.out 000000000040516D Unknown Unknown Unknown
libc.so.6 00007FB8EA5F4D90 Unknown Unknown Unknown
libc.so.6 00007FB8EA5F4E40 __libc_start_main Unknown Unknown
a.out 0000000000405085 Unknown Unknown Unknown
Is this meaningful or is the compiler being overzealous? Perhaps due to disabled optimizations with runtime checks?
链接已复制
OK, but then this doesn't cause the same failure:
program test
implicit none
real, target :: a(100)
integer :: i, ini, fin, n
ini = 1
do i=1,20
n = max(0,min(i,size(a)-ini+1))
fin = ini+n-1
print *,'iter:',i,'elements:',n,'limits:',ini,fin
print *,'items:',a(ini:fin),'sum:',sum(a(ini:fin))
ini = fin+1
end do
end program test
I've quoted an irrelevant fragment of the standard. We are dealing with an array section, not an element. For a subscripts triplet, "the sequence is empty if the first subscript is greater than the second". For an array section "Each subscript ... shall be within the bounds for its dimension unless the sequence is empty".
It is the (superfluous here) bounds remapping list that is confusing the compiler.
