Re: association of pointers



John Harper <harper@xxxxxxxxxxxxx> wrote:

In an attempt to understand pointers, I wrote the following:

PROGRAM testassoc3
INTEGER, TARGET :: beast = 666
INTEGER, POINTER :: ptr => NULL()
ALLOCATE(ptr)
ptr = beast
PRINT "(A,I4)",' After ptr = beast, ptr is',ptr
PRINT "(A,L2)",' associated(ptr,beast) is',associated(ptr,beast)
PRINT "(A,L2)",' associated(ptr) is',associated(ptr)
END PROGRAM testassoc3

The output from four different f95 compilers was

After ptr = beast, ptr is 666
associated(ptr,beast) is F
associated(ptr) is T

That appears to suggest that ptr was associated with something at the
time of printing, but not with beast even though its value was that
of beast. What was ptr associated with?

I've written related stuff about pointers in the past. One of these days
I'll get it down in a more permanent form. From the Fortran 2003
Handbook (no, you can't buy it yet - this is a sneak preview):

In all cases, it is important to understand that a pointer and its
target are distinct entities; their association is temporary. A
pointer does not uniquely "own" its target. There may be multiple
pointers associated with the same target. If any one of those as-
sociations is severed, that does not cause the target to stop
existing; the other pointers would still be associated with the
target. This is often a source of confusion when the ALLOCATE
statement is used to allocate a new target. In that case, the newly
allocated target does not have its own name, but it still has its own
existence. The effect of the ALLOCATE statement is to create an
anonymous target and then to associate the pointer with that target.
Even though the pointer is specified in the ALLOCATE statement that
creates such an anonymous target, the association between that
pointer and that target has no special standing. Other pointers may
subsequently become associated with that target; that pointer may
subsequently become associated with other targets.

Thus, to directly answer your question, ptr is associated with the
anonymous target created by the ALLOCATE statement.

An assignment statement such as ptr=beast does not affect association
(except for components if ptr and beast are derived types, but that's a
deeper matter; you need to get the basics right before going into
matters like that, and the question posed here is very much about
basics). All that assignment statement does is copy the value. If you
wanted to associate ptr with beats, you would use a pointer assignment
statement ptr=>beast. That's what the distinction between = and => is
about.

However, if you did the ALLOCATE, imediately followed by such a pointer
assignment statement, you would just have lost the only way to "get at"
the anonymous target created by the ALLOCATE statement. That is a
trivial example of a classic memory leak. Do that in a loop and your
program will grow indefinitely in size (unless there is an automatic
garbage collector cleaning up behind you).

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: association with zero-length array?
    ... Requirements for pointer and target association ... POINTER attribute, TARGET attribute, pointer association ... If PTR has the POINTER attribute and TGT has the TARGET or ... and TGT considered to be pointer associated, i.e., under which of the ...
    (comp.lang.fortran)
  • association of pointers
    ... INTEGER, TARGET:: beast = 666 ... PRINT "",' After ptr = beast, ptr is',ptr ... END PROGRAM testassoc3 ...
    (comp.lang.fortran)
  • Re: association of pointers
    ... John Harper wrote: ... INTEGER, TARGET:: beast = 666 ... That appears to suggest that ptr was associated with something at the time of printing, but not with beast even though its value was that of beast. ...
    (comp.lang.fortran)
  • Re: TARGET and INTENT(IN)
    ... you can use a pointer to point at something and then modify it through the pointer. ... A pointer is very much like a dummy argument. ... With this meaning of "TARGET" for INTENTdummys I don't see the point - the compiler still doesn't do anything different than for INTENTdummys without TARGET. ... I've been known to have wrapper routines that try to make life simpler for both the routines that calls them and the lower-level routines that they call. ...
    (comp.lang.fortran)
  • Re: Rules for valid pointer deallocation
    ... forrtl: severe: A pointer passed to DEALLOCATE points to an array ... the allocated array is 10 and the extent of the target ... dummy array is also 10, because n is 10 in this ...
    (comp.lang.fortran)