you might like to try the following. This is
an example from our fortran 95 text.
PROGRAM C2004
IMPLICIT NONE
REAL , POINTER :: P1 => NULL()
REAL , POINTER :: P2 => NULL()
ALLOCATE(P1)
P1=21.0
! The value 21.0 is now stored in the
! memory location that P1 points to.
P2=>P1
PRINT *,P1
PRINT *,P2
P2=P2+1
PRINT *,P1
PRINT *,P2
DEALLOCATE(P1)
! This releases the memory and now P1 is
! disassociated.
PRINT *,P2
END PROGRAM C2004
it is an exmaple that we posted some time ago
and things may have improved since then.
even very simple usage of pointers requires
care.
here is another example.
PROGRAM C2009
IMPLICIT NONE
REAL , DIMENSION(:) , POINTER :: X
REAL , DIMENSION(1:10) , TARGET :: Y
INTEGER , PARAMETER :: SIZE=10000
INTEGER :: I
ALLOCATE(X(1:SIZE))
! initialise the memory that x points to
DO I=1,SIZE
X(I)=I
END DO
! print out the first 10 values
DO I=1,10
PRINT *,X(I)
END DO
! initialise the array y
DO I=1,10
Y(I)=I*I
END DO
! print out y
DO I=1,10
PRINT *,Y(I)
END DO
! x now points to y
X=>Y
! print out what x now points to
DO I=1,10
PRINT *,X(I)
END DO
! what has happened to the memory that x
! used to point to?
END PROGRAM C2009
chapter 20 has the majority of the pointer
examples.
our home page is
http://www.kcl.ac.uk/kis/support/cit//fortran/f90home.html
our examples are at
http://www.kcl.ac.uk/kis/support/cit//fortran/f95_examples.html
On Thu, 12 Oct 2000
15:36:52 +0200 Nils Smeds <[log in to unmask]>
wrote:
> Dear fellows,
>
> The program I submitted to the list (Tue, 10 Oct 2000 15:20:34 +0200) is
> not standard conformant. I was a fool thinking that the return argument
> STAT from DEALLOCATE would catch the case when the deallocate object was a
> pointer to an allocatable array.
>
> Now, 5 out of 6 commercial compilers I have tried does flag this in the
> STAT variable. The sixth one does not flag it and instead deallocates the
> target array which is clever too. No compiler I tried blew up my hard disk
> or killed my cat, but they were free to do it from a standard conformant
> point of view.
>
> The standard is what it is (and I haven't bought a copy of it yet). Anyone
> knows if it was ever up to discussion if it was to be an "error condition"
> instead of a user error to use a pointer to an allocated target array as an
> argument to DEALLOCATE? And if there is anything in these lines in the F2k
> discussions?
>
> I enclose a modified version of the earlier code for you to look at and
> test. And imagine that I sent out a non-conforming code to the list and
> didn't get flamed ;-)
>
> /Nils
-------------------
Ian Chivers
[log in to unmask]
* This e-mail message was sent with Execmail V5.0 *
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|