Announcement

Collapse
No announcement yet.

problem with STATIC variables?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with STATIC variables?

    In looking at passing an ASCIIZ PTR back from a function the documentation says that you should declare the corresponding variable in the function as GLOBAL or STATIC. I think this makes sense since when the function terminates you still want the pointer you passed to still point to something. OK...

    The question is does this create a memory leak?

    Every time I call the function it creates a STATIC variable. If I read this correctly the variable (actual data in mem) remains when the function ends and stays viable as long as the program is running. So...does it overwrite this one location each time the function is called or does it allocate a new spot in memory each time the function is called.

    If the latter is true, then would not you eventually eat up memory each time you call the function?

    So...does calling a function with STATIC variables effectively create a memory leak?

  • #2
    STATIC and GLOBAL variables are allocated once, when the program starts, by the compiler (you do nothing).

    Their values may change ("overwritten") , but they are never re-allocated. No leaking.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Originally posted by Mark Farrar View Post
      So...does it overwrite this one location each time the function is called or does it allocate a new spot in memory each time the function is called.
      try it : ? varptr(mystaticvar)

      Comment


      • #4
        thanks

        thanks ...this is what I hoped it would be...

        makes sense...
        M

        Comment


        • #5
          Thank goodness at least one of "the next generation" actually thinks about this kind of thing!
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            next gen?

            next generation?

            LOL

            I'm 46

            Comment


            • #7
              Just like everything else in Windows, "next" is VIRTUAL.
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                "the next generation"
                Methinks that would pertain to any Trekker, that came Before/During/After Trekkie

                Current Gen....lets chalk that up to DS9

                Younger Gen but just now getting there....count as Voyager

                And the kids that already show promise (even at 3 or less) I count as "ENTERPRISE"

                Engineer's Motto: If it aint broke take it apart and fix it

                "If at 1st you don't succeed... call it version 1.0"

                "Half of Programming is coding"....."The other 90% is DEBUGGING"

                "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                Comment


                • #9
                  Fascinating.
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment

                  Working...
                  X