mecej4
Joined: 31 Oct 2006 Posts: 1888
|
Posted: Thu Feb 29, 2024 5:40 pm Post subject: Bugs related to allocatable arrays as subroutine arguments |
|
|
The following test program fails to work correctly when built with FTN95 9.02.
Code: | 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:
Code: | 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:
Code: | 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 "REAL*4 POINTER :: (2)" , and V is shown as "NULL [REAL*4 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). |
|