Consider an old Fortran program that's organised like this:
program main
implicit none
integer rr_sz
parameter(rr_sz=10)
real,dimension(:),allocatable :: rr
allocate(rr(rr_sz))
call sub1(rr_sz/2,rr)
print *, rr_sz, rr
deallocate(rr)
end
subroutine sub1(cc_sz,cc)
implicit none
integer cc_sz,min_cc_sz,i
complex cc
dimension cc(cc_sz)
c here we can determine what array size is really required
min_cc_sz = 6
if (cc_sz.lt.min_cc_sz)
& error stop
do i=1,min_cc_sz
cc(i) = complex(i,i)
end do
end
I would like to put the rr array allocation into sub1, so that when rr needs more space than the hardcoded parameter rr_sz, the program can continue if the system have enough RAM.
It seems like doing so requires an explicit interface for sub1, which makes gfortran fail with a Type mismatch in argument ‘cc’: REAL(4) to COMPLEX(4).
How can I get around this problem with the least possible modifications to the original code?
EDIT
Here's what I tried to do to make the allocation work, that leads to other problems being exposed:
program main
implicit none
integer rr_sz
real,dimension(:),allocatable :: rr
call sub1(rr_sz,rr)
print *, rr_sz, rr
deallocate(rr)
contains
subroutine sub1(rr_sz,cc)
implicit none
integer rr_sz,cc_sz,i
complex,dimension(:),allocatable :: cc
c here we determine what array size is required
cc_sz = 6
allocate(cc(cc_sz))
rr_sz = cc_sz*2
do i=1,cc_sz
cc(i) = complex(i,i)
end do
end
end
# gfortran -fallow-argument-mismatch main.for
main.for:5:72:
5 | call sub1(rr_sz,rr)
| 1
Error: Type mismatch in argument ‘cc’ at (1); passed REAL(4) to COMPLEX(4)
For the past two days I've been reading a lot of discussions about how REAL to COMPLEX casting is "broken", but at the same time people acknowledge that it's expected to work because the standard explicitly states that a COMPLEX shall be stored as two contiguous REALs.
-fallow-argument-mismatch(which I'm guessing is the part of-std=legacyyou're interested in) does not allow argument mismatch when an explicit interface is involved. An explicit interface being required for the allocatable dummy argument makes this approach unuseful.