Re: Pointers and Modules

From: Richard E Maine (nospam_at_see.signature)
Date: 02/23/05


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


Relevant Pages

  • Re: Need help implementing EPA rounding method
    ... distinct binary numbers that all display as decimal 0.5, ... I cannot find a online copy of the standard. ... I suspect the ASTM standard makes no such ... distinction, since the ASTM is not specific to any application (e.g. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: poll() returns POLLIN but no data ?
    ... >> If that's what the standard means, then it seems to me completely ... What does such an assertion mean in any practical sense? ... I think making a distinction between being in a circumstance where you ... No matter what you ...
    (comp.unix.programmer)
  • Re: Alternative Notation
    ... pitch (and/or tone, note) naming system. ... Does anyone know of any other alternative notation systems that use 12 ... music in existence that could be written using standard notation using the ... even aurally is an important distinction. ...
    (rec.music.theory)
  • Re: threads vs. processes
    ... You're making a good distinction, but if you're willing to make this ... The standard says it won't block in the latter ... > has to contend with the backed up load. ... Does the web server require that clients can connect in under 20 ...
    (comp.unix.programmer)