当我尝试将数组传递给另一个数组时,我的代码正在崩溃。我使用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
答案 0 :(得分:3)
可能
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上的表达式是数组部分,而不是可分配数组。