考虑以下子程序
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
!!!!!!!!!!!!!!!!!!!
Run Code Online (Sandbox Code Playgroud)
另外,如何在程序中调用此过程?假设我写
!... some code before
call myProc(5,6,.TRUE.,X)
Run Code Online (Sandbox Code Playgroud)
我是否需要将 X 定义为 (4,6) 实数数组或将可分配数组传递给子例程?
在 Fortran 95 中甚至可以做到所有这些吗?
我创建了一个可分配的数组.我分配元素然后打印数组的大小.我发现奇怪的是,在解除分配后大小保持不变.
Integer, Allocatable :: fred(:)
Allocate (fred(3))
Write (*,*) "fred: ", Size (fred)
Deallocate (fred)
Write (*,*) "fred: ", Size (fred)
Run Code Online (Sandbox Code Playgroud) 假设我们有这个变量定义
Real*8, Dimension(:), Allocatable :: dblA
Allocate (dblA(1000))
Run Code Online (Sandbox Code Playgroud)
现在我调用这个子程序:
Call MySub(dblA)
Run Code Online (Sandbox Code Playgroud)
其中:
Subroutine MySub(dblA)
Real*8, INTENT(Out), DIMENSION(1000) :: dblA
End
Run Code Online (Sandbox Code Playgroud)
这种做法有什么副作用?我应该避免这个吗?