home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.fortran
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!spool.mu.edu!agate!dog.ee.lbl.gov!news!nosc!crash!photon.photon.com!wmc
- From: wmc@photon.com (Bill Cornette)
- Subject: Re: Whats the difference between STATIC variables and SAVE variables?
- Message-ID: <1992Nov9.190426.13544@photon.com>
- Sender: usenet@photon.com (Usenet dummy users)
- Organization: Photon Research Associates, Inc.
- X-Newsreader: TIN [version 1.1 PL6]
- References: <1992Nov7.161830.7705@netcom.com>
- Date: Mon, 9 Nov 1992 19:04:26 GMT
- Lines: 54
-
- John H. Chauvin (jchauvin@netcom.com) wrote:
- : Is there a difference between variables defined as STATIC and those defined
- : with the save statement? How can I tell if a program requires the compiler
- : option STATIC? How does the STATIC option effect program execution speed?
- : Should I try and avoid using it?
-
- The SAVE statement saves (i.e., makes static) either all variables
- within a routine, or those variables so specified. Also, COMMON blocks
- are not static unless the COMMON block name is specified in a SAVE statement
- or the COMMON block is defined in a higher order (relative to the calling
- tree) routine.
-
- I assume that by STATIC, you are refering to a compiler option. IF
- your compiler supports a STATIC command withing the FORTRAN code, it is
- non-ANSI code. The static compiler command will make ALL variables in the
- code static. In other words, the difference is control over the code.
- SAVE works either on a variable by variable or a routine by routine basis,
- while the static command works on whatever is being compiled.
-
- My personal opinion is that SAVE is much more prefereable. I have
- been bitten several times by using imported code that assumes static but
- does not use the SAVE command anywhere. You may remember to compile it
- in a static mode when doing the initial installation, but later, when
- fixing a bug (or when someone else is playing with the code) it may be
- compiled in a dynamic mode (the default on the SGI), with corresponding
- disasterous results.
-
- You cannot tell a code requires STATIC compilation. Some
- imported codes come with instructions to compile it in the static mode,
- but most don't -- IMHO many programmers don't even realize or think about
- the problem (flame on -- that is if you can call such people programmers --
- flame off). You can always run a test case in both static and dynamic modes
- and if the outputs are different, you know that static is required. If the
- outputs are the same you never can really tell if you just got lucky. My
- rule is that ALL imported codes should be compiled static for safety sake.
- In other words, I have learned the hard way not to trust other programmers
- in this respect.
-
- As far as your own codes are concerned, you should learn to check
- if you are assuming that variables not defined in DATA statements (which
- are static unless changed in the code) retain their value from routine call
- to routine call. In that case -- use SAVE. Also, either SAVE all COMMON
- blocks are make sure that they are referenced in a higher routine (e.g.,
- the main PROGRAM).
-
- Obviously (at least to me), STATIC (when not required) takes extra
- execution time. The code must save all variables in a stack somewhere and
- reinitialize all variables whenever entering a routine. I would avoid
- it in all home-grown programs for this reason, but as noted above, you
- may want to use it routinely for all imported codes.
-
- Hope this helps.
-
- ---- Bill Cornette
-