Re: Pointers and Modules
From: Richard E Maine (nospam_at_see.signature)
Date: 02/23/05
- Next message: Michael Metcalf: "Re: Pointers and Modules"
- Previous message: Gordon Sande: "Re: Question about modules and namelists"
- In reply to: ajs: "Pointers and Modules"
- Next in thread: James Van Buskirk: "Re: Pointers and Modules"
- Reply: James Van Buskirk: "Re: Pointers and Modules"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 23 Feb 2005 08:18:48 -0800
In article <1109166931.805933.308610@o13g2000cwo.googlegroups.com>,
"ajs" <ajs00g@gmail.com> wrote:
> Is the following code correct? In other words, is the pointer xptr
> preserved between the call to setptr and useptr? Is it supposed to be?
[code elided]
I'm not entirely sure I understand the distinction between the two
questions (of whether it is preserved vs whether it is supposed to be),
though I have 2 answers that might go with the 2 questions.
First I'll interpret whether it is supposed to be as a question about
what the standard specifies. The standard specifies that xptr
becomes undefined when nothing in the module is being called. That is,
no, xptr is not preserved. The code is non-standard because it
references a pointer with undefined association status.
Note that you can *NOT* use the associated intrinsic to test for
undefined association status. The associated intrinsic is *ONLY* good
for distinguishing between associated vs disassociated, when the
possibility of undefined is ruled out. So your use of the associated
intrinsic doesn't help. In fact, the associated test in
if(associated(xptr)) nullify(xptr)
is pretty much pointless. This is directly parallel to the non-pointer
if (i/=0) i = 0
Simpler to just dispense with the test.
To make the pointer preserved, and thus the code standard-conforming,
you need the SAVE attribute. THis is exactly the kind of thing that SAVE
is for. Pointers are pretty parallel to non-pointers in this regard.
I'll interpret the question about whether it is preserved as asking
about what happens in practice with typical compilers. In practice, I
don't know of a current compiler where module and common variables
aren't effectively saved anyway. That is, the code will probably work.
But that doesn't mean that it is standard-conforming and guaranteed to
work - just that it will probably work anyway.
I recommend specifying SAVE to make the code standard. It's negligible
bother (particularly once you start getting in the habit of it). The
fact that it makes you think about whether a variable needs to be saved
or not is, IMO, a good thing because one should think about that. If you
ever need to do something like make the code so that multiple instances
can be defined and active at once, the variables that need to be saved
will be ones that need special attention.
P.S. I'm ignoring part of the question that you didn't ask, because I
don't recall the answer without checking carefully. I always get
confused about target dummy arguments. I'm not 100% sure whether xptr
gets "properly" associated with the actual argument in the first place.
I think it does, but I'm not sure. James Van Buskirk would know (and
would probably wonder why I had trouble remembering the rules) :-).
My answer above is just addressing the issue of whether the association
status is preserved between calls - not the issue of whether it was set
up correctly in the first place.
P.P.S.
> subroutine freeptr
> if(associated(xptr)) nullify(xptr)
With the SAVE, the above will be valid, but I'd find the subroutine name
slightly misleading, which makes me wonder whether you know what it
does. This does not free any memory; x remains allocated. All this does
is stop xptr from pointing to it. If you actually wanted to free the
memory, you'd need a deallocate instead of a nullify.
-- Richard Maine | Good judgment comes from experience; email: my first.last at org.domain | experience comes from bad judgment. org: nasa, domain: gov | -- Mark Twain
- Next message: Michael Metcalf: "Re: Pointers and Modules"
- Previous message: Gordon Sande: "Re: Question about modules and namelists"
- In reply to: ajs: "Pointers and Modules"
- Next in thread: James Van Buskirk: "Re: Pointers and Modules"
- Reply: James Van Buskirk: "Re: Pointers and Modules"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|