Re: association of pointers
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Tue, 13 Nov 2007 16:18:14 -0800
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
.
- Follow-Ups:
- Re: association of pointers
- From: jtravs
- Re: association of pointers
- References:
- association of pointers
- From: John Harper
- association of pointers
- Prev by Date: speed up calculation suggestions
- Next by Date: Re: association of pointers
- Previous by thread: Re: association of pointers
- Next by thread: Re: association of pointers
- Index(es):
Relevant Pages
|