- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The Fortran language requires that "The value of a subscript in an array element shall be within the bounds for its dimension."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
A bug report has been opened for the error in the first case.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page