Could you help out with the following example?
Jane Sleightholme and I have been looking at simple
examples to illustrate dangling pointers and memory
leaks.
PROGRAM C20_04
IMPLICIT NONE
REAL , POINTER :: P2=>null()
interface
subroutine test(p2)
implicit none
REAL , POINTER :: P2
end subroutine test
end interface
print *,' staring main program'
if (associated(p2)) then
print *,' p2 is associated '
end if
call test(p2)
if (associated(p2)) then
print *,' p2 is associated '
end if
print *,' p2 in main program = ',p2
print *,' returning from test routine'
end program c20_04
subroutine test(p2)
implicit none
REAL , POINTER :: P1
REAL , POINTER :: P2
integer :: istat
integer :: t
print *,' entering test'
allocate(p1)
P1=21.0
! The value 21.0 is now stored in the
! memory location that P1 points to.
P2=>P1
! verify that they are the same
PRINT * , ' p1 = ',P1
PRINT * , ' p2 = ',P2
p2=p1+1
! verify that they are both the same
PRINT * , ' p1 = ',P1
PRINT * , ' p2 = ',P2
! Now release the memory.
! P1 becomes disassociated.
deallocate(p1,stat=istat)
! check the status of the deallocate
print *,' verify deallocate status ',istat
if (associated(p1)) then
print *,' p1 is associated '
end if
if (associated(p2)) then
print *,' p2 is associated '
end if
! what does p2 point to?
PRINT * , ' p2 = ', P2
p2=p2+1
PRINT * , ' p2 = ', P2
print *,' about to leave test'
! comment out the following statement and run again.
read *,t
END subroutine test
we presume that you need to nullify p2 before
deallocating p1.
we've tried the example as above but without the
input prompt and got different behaviour regarding p2.
we've also taken out the
=>null()
and tried with some fortran 90 compilers.
thanks in advance
Ian Chivers
Jane Sleightholme
King's College London
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|