association of pointers

  • Follow


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?
 
-- John Harper, School of Mathematics, Statistics and Computer Science, 
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
0
Reply harper (718) 11/13/2007 11:09:02 PM

John Harper wrote:

> PROGRAM testassoc3
>   INTEGER, TARGET  :: beast = 666

beast has value 666 and may be the target of a pointer

>   INTEGER, POINTER :: ptr  => NULL()
>   ALLOCATE(ptr)

A new variable is allocated, pointed to by ptr.

>   ptr = beast

The value of beast is copied to the variable pointed
to by ptr.

>   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)

ptr and beast point to different variables, so are not associated.

>  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?

Instead of allocate(ptr) do

    ptr => beast

-- glen

0
Reply gah (12303) 11/14/2007 12:16:39 AM


John Harper <harper@mcs.vuw.ac.nz> 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
0
Reply nospam47 (9742) 11/14/2007 12:18:14 AM

John Harper 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?
>  
> -- John Harper, School of Mathematics, Statistics and Computer Science, 
> Victoria University, PO Box 600, Wellington 6140, New Zealand
> e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045

Have you considered the differences between

   ptr => beast

and

   ptr = beast

?

-- mecej4
0
Reply mecej4 (109) 11/14/2007 12:21:28 AM

On Nov 14, 12:18 am, nos...@see.signature (Richard Maine) wrote:
> 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):

Hi, this is the first I have heard of the Fortran 2003 Handbook. I
presume it's a book by you? Out of interest do you have a plan of when
it might be available? I'd very much like to get a copy.

Best regards,
John

0
Reply jtravs (11) 11/14/2007 7:35:10 PM

jtravs wrote:
> On Nov 14, 12:18 am, nos...@see.signature (Richard Maine) wrote:
>> 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):
> 
> Hi, this is the first I have heard of the Fortran 2003 Handbook. I
> presume it's a book by you? Out of interest do you have a plan of when
> it might be available? I'd very much like to get a copy.

Not to preempt one of the co-authors, but I also am waiting. Until then have a lookee at:

http://www.springer.com/west/home/computer/programming?SGWID=4-40007-22-141350063-0

(and also any amazon.com besides the US one. All the "other" amazons have it listed as "to 
be published". But not the US one. Weird.)

cheers,

paulv
0
Reply paul.vandelst (1947) 11/14/2007 8:23:18 PM

jtravs <jtravs@gmail.com> wrote:

> On Nov 14, 12:18 am, nos...@see.signature (Richard Maine) wrote:
> > 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):
> 
> Hi, this is the first I have heard of the Fortran 2003 Handbook. I
> presume it's a book by you? Out of interest do you have a plan of when
> it might be available? I'd very much like to get a copy.

Springer says May. And I think we are actually on schedule (the current
one). It's been slow. :-(

The writing is done. Just needs final editing, index, and then whatever
Springer does to get it out the door.

-- 
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain
0
Reply nospam47 (9742) 11/14/2007 11:37:48 PM

6 Replies
40 Views

(page loaded in 0.174 seconds)


Reply: