(我在19.0.5.281版本中使用(
在下面的测试代码中,在move_association
中,我将POINTER作为INTENT(In(,并检查其关联状态,并将其关联更改为局部变量array(4,4)
。
module mod_test
implicit none
contains
subroutine write_matrix_2d(a)
implicit none
real, intent(in) :: a(:,:)
! local variables
integer :: i, j
do i = lbound(a,1), ubound(a,1)
write(*,*) ( a(i,j), j = lbound(a,2), ubound(a,2) )
enddo
end subroutine
(*) subroutine move_association(a)
implicit none
real, pointer, intent(inout) :: a(:,:)
! local
real, target :: array(4,4)
array = -1
if ( associated(a) ) then
print *, "POINTER is associated"
a => array
else
print *, "POINTER is NOT associated"
a => array
endif
end subroutine
end module
program test
use mod_test
implicit none
real, dimension(3,3), target :: ct(3,3)
real, dimension(:,:), pointer :: cptr(:,:)
ct = 1
!cptr => ct(2:3,2:3)
write(*,*)
write(*,*) 'ct'
call write_matrix_2d(ct)
write(*,*)
write(*,*) 'cptr before'
call write_matrix_2d(cptr)
write(*,*)
(*) call move_association(cptr)
write(*,*)
write(*,*) 'cptr after'
call write_matrix_2d(cptr)
write(*,*)
if ( associated(cptr) ) then
print *, " cptr associated "
else
print *, " cptr not associtated "
endif
end program
乍一看,我认为应该有一些问题,因为Fortran在子程序结束时自动释放子程序的局部变量,使cptr
失去目标。但出乎意料的是CCD_ 4存活下来;关联的";作为以下输出。
ct
1.000000 1.000000 1.000000
1.000000 1.000000 1.000000
1.000000 1.000000 1.000000
cptr before
POINTER is NOT associated
cptr after
-1.000000 -1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000 -1.000000
cptr associated
我想可能还有其他规则,比如子程序中的局部target
变量自动获得save
属性等等,对吗?
指针在末尾的状态为undefined
。不允许查询未定义状态的指针的关联状态,它包含一些垃圾。垃圾恰好是不再有效的本地数组。在进行进一步调查之前,您必须将其作废或自己关联。
另请参阅http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html#5