Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29007 Discussions

Zero length pointer to "out of bounds" element

Jellby1
New Contributor I
360 Views

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?

0 Kudos
4 Replies
IanH
Honored Contributor III
295 Views

The Fortran language requires that "The value of a subscript in an array element shall be within the bounds for its dimension."

0 Kudos
Jellby1
New Contributor I
256 Views

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
0 Kudos
IanH
Honored Contributor III
241 Views

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.

0 Kudos
Xiaoping_D_Intel
Employee
145 Views

A bug report has been opened for the error in the first case.


Reply