On Mon, 27 Mar 2000 10:32:14 +0100 (BST) John Reid
<[log in to unmask]> wrote:
> > 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)
>
> At this point, the pointer association status of p2 becomes undefined.
> This means that from here on, you should make no further reference to
> it until the pointer association status is defined by a pointer
> association, allocation, or nullification. There is no requirement on
> the processor to check this.
>
> Hope this helps,
>
> John Reid.
thanks. this is the output we've got from 4 compilers.
NAG f95, Sun Ultrasparc
staring main program
entering test
p1 = 21.0000000
p2 = 21.0000000
p1 = 22.0000000
p2 = 22.0000000
verify deallocate status 0
p2 is associated
p2 = 22.0000000
p2 = 23.0000000
about to leave test
p2 is associated
p2 in main program = 23.0000000
returning from test routine
Sun f90 as above
staring main program
entering test
p1 = 21.0
p2 = 21.0
p1 = 22.0
p2 = 22.0
verify deallocate status 0
p2 is associated
p2 = 22.0
p2 = 23.0
about to leave test
p2 is associated
p2 in main program = 2.316963E-40
returning from test routine
Nagace f90, as above
staring main program
entering test
p1 = 21.0000000
p2 = 21.0000000
p1 = 22.0000000
p2 = 22.0000000
verify deallocate status 0
p2 is associated
p2 = 22.0000000
p2 = 23.0000000
about to leave test
p2 is associated
p2 in main program = 23.0000000
returning from test routine
Compaq/dec f95, pc
staring main program
entering test
p1 = 21.00000
p2 = 21.00000
p1 = 22.00000
p2 = 22.00000
verify deallocate status 0
p2 is associated
p2 = 22.00000
p2 = 23.00000
about to leave test
p2 is associated
p2 in main program = 0.0000000E+00
returning from test routine
you'll get slightly different results if you take out the read
statement with one of the compilers.
-------------------
Ian Chivers
[log in to unmask]
* This e-mail message was sent with Execmail V5.0 *
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|