Silverfrost Forums

Welcome to our forums

Bugs related to allocatable arrays as subroutine arguments

29 Feb 2024 4:40 #31173

The following test program fails to work correctly when built with FTN95 9.02.

program allocarg                 ! Expects compiler to support '(re)allocate on assignment'
   implicit none
   real, allocatable :: u(:), v(:)
!
   allocate (u(2))
   u = [1.0,2.0]
   call sub (u,v)
   print 20,'After return from SUB,    u = ',u
   print 20,'   and                    v = ',v
 20 format(1x,A,T35,2f4.1)

CONTAINS
   subroutine sub(p,q)
   implicit none
   real, dimension(:), allocatable :: p, q
!
   if (allocated(p)) print 10,'p is allocated, size = ',size(p)
   if (allocated(q)) print 10,'q is allocated, size = ',size(q)

   q = p*3  ! q is not allocated earlier, but allocate-on-assignment makes statement OK

   print 20,'After assigning q = p*3, v = ',v
 10 format(1x,A,T35,2i4)
 20 format(1x,A,T35,2f4.1)
   return
   end subroutine
end program

The expected output:

 p is allocated, size =              2
 After assigning q = p*3, v =     3.0 6.0
 After return from SUB,    u =     1.0 2.0
    and                    v =     3.0 6.0

When compiled by FTN95, with or without /64, the program runs into 'Access Violation: attempted to write to location 00000000.

When compiled with /check, the 32-bit EXE aborts with ' Nonconformant arrays on line 20', and the 64-bit EXE built with /check gives the output:

              p is allocated, size =              2
              After assigning q = p*3, v =
              After return from SUB,    u =     1.0 2.0
              and                       v =

Note the blanks in the last output line after 'v ='.

When the program is built with /debug and run in SDBG/SDBG64, more defects are seen.

When line-6 has been executed, U is shown in the Variables pane of SDBG64 as 'REAL4 POINTER :: (2)' , and V is shown as 'NULL [REAL4 POINTER :: (Subscripts not valid)]'. A user may prefer to have allocatable arrays treated not in terms of pointers but in terms of attributes such as 'allocated', 'size', etc. (even if pointers underlie the compiler's implementation of allocatable arrays).

On compilation with /debug and entering SUB in SDBG-32, P is shown in the Variables pane as REAL*4 (4214812:8429624).

19 Mar 2024 1:03 #31287

mecej4

Thank you for this bug report which I some how missed.

14 May 2024 2:12 #31348

mecej4

This failure has been fixed in the latest release v9.03 of FTN95 except that

the print statement

print 20,'After assigning q = p*3, v = ',v

must be changed to

print 20,'After assigning q = p*3, q = ',q

This is because v is passed as an argument and (for FTN95) does not have a size until after the return from the call to sub.

The original code appears to be accepted by other compilers such as gFortran but I think that the way FTN95 works in this context makes sense.

14 May 2024 3:02 #31350

Thank you, I checked out the new version and it works.

Please login to reply.