我想知道在现代 Fortran 中是否可以使用其本身或其中的一部分来分配可分配数组来执行此操作。这是一个简单的例子:
module modu
implicit none
type :: t
integer :: i
end type
contains
subroutine assign(a,b)
type(t), allocatable, intent(out) :: a(:)
type(t), intent(in) :: b
allocate(a(1))
a(1) = b
end subroutine
end module
!----------------------
program test
use modu
implicit none
type(t), allocatable :: a(:)
allocate(a(1))
a(1)%i = 2
call assign(a, a(1))
print*, a(1)%i
end program
Run Code Online (Sandbox Code Playgroud)
此代码使用 ifort 18 给出正确的答案,并使用 gfortran 7.4 返回“分段错误”。
注意:原来的问题有点复杂,因为call assign(a, a(1))应该call assign(a, a(1)+b)用运算符 + 正确重载来替换,但结论(尊重 ifort 和 gfortran)是相同的。