home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / fortran / 4254 < prev    next >
Encoding:
Text File  |  1992-11-10  |  3.4 KB  |  67 lines

  1. Newsgroups: comp.lang.fortran
  2. 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
  3. From: wmc@photon.com (Bill Cornette)
  4. Subject: Re: Whats the difference between STATIC variables and SAVE variables?
  5. Message-ID: <1992Nov9.190426.13544@photon.com>
  6. Sender: usenet@photon.com (Usenet dummy users)
  7. Organization: Photon Research Associates, Inc.
  8. X-Newsreader: TIN [version 1.1 PL6]
  9. References: <1992Nov7.161830.7705@netcom.com>
  10. Date: Mon, 9 Nov 1992 19:04:26 GMT
  11. Lines: 54
  12.  
  13. John H. Chauvin (jchauvin@netcom.com) wrote:
  14. : Is there a difference between variables defined as STATIC and those defined
  15. : with the save statement? How can I tell if a program requires the compiler
  16. : option STATIC? How does the STATIC option effect program execution speed?
  17. : Should I try and avoid using it?
  18.  
  19.     The SAVE statement saves (i.e., makes static) either all variables
  20. within a routine, or those variables so specified.  Also, COMMON blocks
  21. are not static unless the COMMON block name is specified in a SAVE statement
  22. or the COMMON block is defined in a higher order (relative to the calling
  23. tree) routine.
  24.  
  25.     I assume that by STATIC, you are refering to a compiler option.  IF
  26. your compiler supports a STATIC command withing the FORTRAN code, it is
  27. non-ANSI code.  The static compiler command will make ALL variables in the
  28. code static.  In other words, the difference is control over the code.
  29. SAVE works either on a variable by variable or a routine by routine basis,
  30. while the static command works on whatever is being compiled.
  31.  
  32.     My personal opinion is that SAVE is much more prefereable.  I have
  33. been bitten several times by using imported code that assumes static but
  34. does not use the SAVE command anywhere.  You may remember to compile it
  35. in a static mode when doing the initial installation, but later, when
  36. fixing a bug (or when someone else is playing with the code) it may be
  37. compiled in a dynamic mode (the default on the SGI), with corresponding
  38. disasterous results.
  39.  
  40.     You cannot tell  a code requires STATIC compilation.  Some
  41. imported codes come with instructions to compile it in the static mode,
  42. but most don't -- IMHO many programmers don't even realize or think about
  43. the problem (flame on -- that is if you can call such people programmers -- 
  44. flame off).  You can always run a test case in both static and dynamic modes
  45. and if the outputs are different, you know that static is required.  If the
  46. outputs are the same you never can really tell if you just got lucky.  My
  47. rule is that ALL imported codes should be compiled static for safety sake.
  48. In other words, I have learned the hard way not to trust other programmers
  49. in this respect.
  50.  
  51.     As far as your own codes are concerned, you should learn to check
  52. if you are assuming that variables not defined in DATA statements (which
  53. are static unless changed in the code) retain their value from routine call
  54. to routine call.  In that case -- use SAVE.  Also, either SAVE all COMMON
  55. blocks are make sure that they are referenced in a higher routine (e.g.,
  56. the main PROGRAM).
  57.  
  58.     Obviously (at least to me), STATIC (when not required) takes extra 
  59. execution time.  The code must save all variables in a stack somewhere and
  60. reinitialize all variables whenever entering a routine.  I would avoid
  61. it in all home-grown programs for this reason, but as noted above, you
  62. may want to use it routinely for all imported codes.
  63.  
  64. Hope this helps.
  65.  
  66. ----  Bill Cornette
  67.