Hello,
At 16:14 26-03-2000 +0100, you wrote:
>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 *,' starting main program'
! NULLIFY(p2) ! Fortran 90 [JvO]
> 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.
Indeed I got different results with different systems.
>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.
Output from Absoft f90 V6:
Starting main program
Entering test
p1 = 21.0000
p2 = 21.0000
p1 = 22.0000
p2 = 22.0000
Verify deallocate status 0
p2 is associated
p2 = 1.811806E-38
p2 = 1.00000
About to leave test
p2 is associated
p2 in main program = 1.00000
Returning from test routine.
Output from Salford FTN95 v2.0
Starting main program
Entering test
p1 = 21.0000
p2 = 21.0000
p1 = 22.0000
p2 = 22.0000
Verify deallocate status 0
p2 is associated
p2 = 22.0000
p2 = 23.0000
About to leave test
p2 is associated
p2 in main program = 23.0000
Returning from test routine.
---
Meilleures Salutations,
Best Greetings,
/---
Jan van Oosterwijk
Computing Centre
Delft University of Technology
Postbus 354
2600 AJ Delft
Netherlands / Pays-Bas
Phone: +31 15 278 50 17
Fax: +31 15 278 37 87
|