当我试图将一个数组传递给另一个数组时,我的代码当前正在中断。我使用gfortran作为没有特殊标志的编译器。我也把这个文件写成".f95">
program foo
integer, parameter :: pp = 16
call fooyoo
contains
subroutine fooyoo
real(kind=pp), allocatable :: f(:,:,:), y_ix(:), r(:)
integer :: ii, jj, nn
nn = 32
r = zoo(nn)
y_ix = r ! this is done because I'm actually allocating two other 1D arrays
do ii = 1, nn
do jj = 1, nn
write(*,*) "crashes here"
f(ii,jj,:) = y_ix
write(*,*) "nothing here"
end do
end do
end subroutine
function zoo(nn) result(r)
integer :: i, nn
real(kind=pp) :: r(nn)
do i = 1, nn
r(i) = i
end do
end function
end program
可能是
do ii = 1, nn
do jj = 1, nn
write(*,*) "crashes on executing the next line because programmer forgot ", &
"to allocate f before trying to set values"
f(ii,jj,:) = y_ix
write(*,*) "nothing here"
end do
end do
在这一行
y_ix = r
自动分配之所以有效,是因为lhs上的表达式是一个可分配的数组。在中
f(ii,jj,:) = y_ix
自动分配失败,因为lhs上的表达式是一个数组部分,而不是可分配的数组。