comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » STATIC VARIABLES
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
STATIC VARIABLES [message #2690] Thu, 18 August 1994 08:46 Go to next message
sims is currently offline  sims
Messages: 1
Registered: August 1994
Junior Member
I wanted to know if anyone has used static type variables in IDL functions and if
so how.
Re: static variables [message #7118 is a reply to message #2690] Tue, 01 October 1996 00:00 Go to previous messageGo to next message
Peter Mason is currently offline  Peter Mason
Messages: 145
Registered: June 1996
Senior Member
On 30 Sep 1996, Esfandiar Bandari wrote:
> I am newcomer to idl and this news group! I was wondering if
> there is any clean way of having static variables inside files or
> functions (as in C or C++). These are variables that keep their value
> from function call to function call and are local to the function.

The variables you define inside a function (or procedure) get undefined
once the function is exited. (In IDL you (re)define a variable just by
(re)assigning a value to it.) So there is no way to have static local
variables, and:
** you have to resort to some sort of global kludge **

One way is to use common blocks. For example, you could define a common
block just for the function - you'd define and use the common block in
the function and nowhere else. (Common blocks in IDL are like the ones in
Fortran. They're really meant for providing global access to "common"
variables. Here we're using a side effect of theirs - they're static.)
This is probably the closest you'll get to local static vars.

Another way is to pass all the variables into the function. (Perhaps you'd
keep the function's "static" variables in a structure so that you only have
to pass one thing.) This would mean that you have to pass this stuff all
the way down from the main level to the function concerned. Not that good.

You might also want to check out "handles" and "widget uvalues" in the docs,
although they might be out of context here. I'm thinking that if the
function in which you need static locals is called from a widget event handler
of yours, then uvalues are actually ideal for this sort of thing. Basically,
the event handler would provide your function with a couple of widget IDs
including the ID of a "parent" widget - an ideal place to stash "static"
stuff.
(Programmers often store a structure of "state" variables in a parent
widget's uvalue. It's a very convenient method as a widget's uvalue
stays alive (and just the way you left it) as long as the widget is alive,
and the ID of the parent widget is available in "event.top", where "event" is
a scalar structure variable which is automatically passed to your event
handler. You could even store your state in one of the "child widgets" of
the parent - check out /CHILD and /SIBLING under WIDGET_INFO(), and have a
look at some of the "CW_*.PRO" programs in the IDL distribution. Handles
work similarly, except that you have to keep track of handle IDs on your
own.)
Just a last note on handles and widget uvalues:
These work similarly to C pointers (pointers to anything). The difference is
that you can't reference things indirectly - you have to actually retrieve the
stuff pointed to first, before you can work with it. There are two ways to
retrieve. The standard way (no special switches) COPIES stuff from the
pointer, leaving the pointer still pointing to a copy of its own; if you
change the stuff then you have to put it back to "update" the pointer's copy.
The efficient way involves the use of the /NO_COPY keyword - it leaves the
pointer pointing to nothing, and you always have to replace the stuff when
you're finished working with it.
(The "efficient" way doesn't incur wastage of memory due to extra copies of
variables - something which can become an issue in IDL programs.)
I hope this all makes sense to you.


Peter Mason
Re: static variables [message #7119 is a reply to message #2690] Mon, 30 September 1996 00:00 Go to previous messageGo to next message
Eric Deutsch is currently offline  Eric Deutsch
Messages: 11
Registered: May 1995
Junior Member
Esfandiar Bandari wrote:
>
> Hi:
> I am newcomer to idl and this news group! I was wondering if
> there is any clean way of having static variables inside files or
> functions (as in C or C++). These are variables that keep their value
> from function call to function call and are local to the function.

I think your only option is to use common blocks. e.g.:

function calc1,var1,var2
common COM_CALC1,var3,var4
if (n_elements(var3) eq 0) then var3=0.0
if (n_elements(var4) eq 0) then var4=50.0
if (n_elements(var2) ne 0) then var4=var2
print,var3
var3=var3+1
return,var1*var2*var3
end

If calc1 is the only function that ever uses the common block COM_CALC1
then, var3,var4 work essentially like static variables. Note that there
is
the option of other functions/procedures also using this common
block which then makes var3,var4 a little bit more like C global
variables.
Also, IDL won't let you stuff var1,var2 into a common block. In the
needlessly-complicated example above, the function automatically starts
counting the number of times it gets called in an IDL session, and var4
can be specified in the function call, or if left unspecified in the
function call, takes on the value from the previous call (or the default
of 50.0)

hope this helps,
Eric

--
------------------------------------------------------------ ----------------
Eric Deutsch email:
deutsch@astro.washington.edu
Department of Astronomy Voice: (206)
616-2788
University of Washington FAX: (206)
685-0403
Box 351580 WWW:
http://www.astro.washington.edu/deutsch/
Seattle, WA 98195-1580 Physics/Astronomy Bldg., Room
B356F
Re: static variables [message #8887 is a reply to message #2690] Wed, 30 April 1997 00:00 Go to previous message
peter is currently offline  peter
Messages: 80
Registered: February 1994
Member
Jonathan Rogness (rogness@sg1.cr.usgs.gov) wrote:
: Sorry to answer my own question, but I had forgotten IDL supports global
: variables using Common Blocks. All my training as a programmer tells me
: not to use global vars, but if it works then I guess I can deal with it.
: :-)

Well, you're in luck! Variables in common blocks are not global, merely
static; they have scope only over the functions in which the common
block is declared. This is true even for the main level, the common
block has to be declared there for the variables to exist. In general,
the same name can be reused inside different common blocks, provided
they aren't both used in the same namespace.

Peter
Re: static variables [message #8893 is a reply to message #2690] Wed, 30 April 1997 00:00 Go to previous message
Jonathan Rogness is currently offline  Jonathan Rogness
Messages: 7
Registered: April 1997
Junior Member
Sorry to answer my own question, but I had forgotten IDL supports global
variables using Common Blocks. All my training as a programmer tells me
not to use global vars, but if it works then I guess I can deal with it.
:-)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: static variables
Next Topic: passing parameters on to other functions

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:35:40 PDT 2025

Total time taken to generate the page: 0.00657 seconds