考虑以下子例程
subroutine myProc(m,n,flag,X)
Integer, intent(in) :: m,n
logical, intent(in) :: flag
real(8), intent(out), allocatable :: X(:,:)
if (flag) then
allocate(X(m,n))
! some more code here
else
allocate(X(m-1,n))
! some more code here
end if
end subroutine myProc
!!!!!!!!!!!!!!!!!!!
另外,如何在程序中调用这个过程?假设我写
!... some code before
call myProc(5,6,.TRUE.,X)
我是否需要将 X 定义为 (4,6) 实数组或将可分配数组传递给子例程?
这一切在 Fortran 95 中是否可能实现?
在 Fortran 2003 及更高版本中,将可分配的虚拟参数传递给子例程是完全可以的。 IIRC 首先由 Fortran 95 的 TS(技术规范)允许。今天所有编译器都支持它。
当实际参数传递给可分配的虚拟参数时,不必分配实际参数。这里,如果碰巧分配了它,它实际上会被自动释放,因为虚拟对象是intent(out)
and allocatable, intent(out)
进入子例程时,参数会自动释放。
您的代码应该可以正常工作,但有一个重要的方面,子例程必须具有显式接口。所以必须放在一个模块中。或者它必须是内部的(之后contains
) or an interface
必须使用块(丑陋)。
你可以像你展示的那样调用它
call myProc(5,6,.TRUE.,X)
and X
不必分配。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)