Silverfrost Forums

Welcome to our forums

FTN95 fails to catch error with INTENT(IN) of TARGET array

18 Oct 2016 12:24 #18160

Once in a while one has to contend with POINTER variables in someone else's Fortran code that was written before ALLOCATABLE array arguments became available. Such variables can lead to puzzling errors, and FTN95 is good at helping to catch them. Here is one case where /CHECKMATE is not enough to catch a bug.

program xptr
implicit none
real, pointer :: xa(:)
real :: s
allocate(xa(5))
xa=[5.0,1.0,4.0,2.0,3.0]
call sub(xa,s)
write(*,*)s,sum(xa)
write(*,*)xa

CONTAINS

subroutine sub(y,s)
real, intent(in), target :: y(:)  ! Intent violated below
real, intent(out) :: s
real, pointer :: z(:)
z => y(2:4)                       ! Array section
z=z*2                             ! Modify section
s=sum(y)
return
end subroutine sub

end program xptr

The output with FTN95 is

      22.0000         15.0000
      5.00000         1.00000         4.00000         2.00000         3.00000

In effect, FTN95 is passing a copy of the array, and does not catch the modification of elements of the array in the subroutine, despite the INTENT(IN) attribute.

18 Oct 2016 3:20 #18163

Thank you for the feedback. I have made a note of this.

13 Jul 2019 8:32 #23990

mecej4

I apologise for the long delay in responding to this enquiry.

It is true that FTN95 runtime checking does not detect this programming error. Unfortunately this type of error is way beyond what the current mechanism can cope with.

As you note, FTN95 takes a copy of the INTENT(IN) array so the integrity of the original array is preserved and the result is in fact logically correct and may even be what the author of the code intended.

Please login to reply.