【发布时间】:2023-02-08 17:22:52
【问题描述】:
我在 Stack Overflow 上的许多帖子中读到,当一个可分配数组被传递到一个子例程中时,它被释放,其中虚拟参数是 intent(out)。
如果我考虑以下代码:
program main
real, dimension(:), allocatable :: myArray
integer :: L=8
allocate(myArray(1:L))
call initArray(myArray)
print *, myArray
contains
subroutine initArray(myArray)
real, dimension(:), intent(out) :: myArray
myArray(:) = 10.0
end subroutine initArray
end program main
输出是正确的。因此,当发生释放时,内存被释放但数组形状保持不变。准确吗?任何详细的解释将不胜感激。
我阅读了关于该主题的不同帖子(Can I use allocatable array as an intent(out) matrix in Fortran?、What is the effect of passing an allocatable variable into a subroutine with non-allocatable argument?、...)。所以我知道数组已被释放,但我想了解它是什么意思,因为在我的代码中,大小保持不变,我也很惊讶这段代码有效。
【问题讨论】: