Announcement

Collapse
No announcement yet.

how static are they?

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

  • how static are they?

    Hello,

    I was just wondering, If you were to "dim" a static variable in
    a function and have that function return a pointer to the variable,
    is it safe to use that pointer throughout your program or will the
    "dimmed" variable move around in memory and thus invalidate the
    pointer?



    ------------------
    Cheers

  • #2
    I can tell you that that won't work if the static variable is a dynamic string. They move around every time their value is changed.

    But as for numerics... It seems like it would work. However, I wouldn't rely on it without an official word from support or R&D.

    A guaranteed way to do it would be to create a GLOBAL variable with a unique name that no other sub or function declares. That would accomplish the same thing...

    -- Eric

    ------------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>

    "Not my circus, not my monkeys."

    Comment


    • #3
      Mark,

      Eric has hit this one square on the head, a STATIC variable is useful if
      you need a value that remains constant within a function/sub each time
      you call it.

      If you need to access the variable from outside of the function/sub where
      it is allocated, a GLOBAL variable has the scope to do that in that it can
      be "seen" from anywhere in the program.

      The scope of variables in PowerBASIC is very flexible and you have exactly
      what you need in the 3 forms that are available,

      GLOBAL can be seen from anywhere in the application and remains constant.

      STATIC can ONLY be seen from within a function/sub but remains constant.

      LOCAL (or DIM) can only be seen from within a function/sub and it is
      transient. It is available ONLY in the function/sub where it is allocated
      and it does not retain its value after the function/sub is closed. LOCAL
      variables are allocated on the stack and can be treated as dynamic variables.

      Regards,

      [email protected]

      ------------------
      hutch at movsd dot com
      The MASM Forum

      www.masm32.com

      Comment


      • #4
        Eric and Steve are quite correct. Maybe it would be a better idea to just use a GLOBAL for simplicity sake?

        To add to Steve's comments; LOCAL arrays and dynamic (variable length) strings are not allocated on the stack in a Sub/Function, all other LOCAL variables are.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Originally posted by mark smit:
          I was just wondering, If you were to "dim" a static variable in
          a function and have that function return a pointer to the variable,
          is it safe to use that pointer throughout your program or will the
          "dimmed" variable move around in memory and thus invalidate the
          pointer?
          This is among the traditional uses of static variables. It's perfectly safe as long as you remember that the value has to be used before the function is called again, or you won't get the result you were expecting.

          It's good to keep in mind that such functions are not reentrant.

          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment


          • #6
            Hey Tom,

            What exactly do you mean by reentrant?

            The whole idea I had was to make a database DLL that would handle
            all the data tables internally. Maybe I should give an example...

            Code:
            type CBitRecord
                m_WellNumber as long
                m_Pointer as long
                m_BitNumber as long
            end type
            
            function BitRecord(byval cmd as long,byval Index as long, Record as CBitRecord) export as long
                dim BitRecordTable() as static CBitRecord
            
                select case (cmd)
                    case %GETRECORD
                        Record = BitRecordTable(Index)
                        function = ubound(BitRecordTable)
            
                    case %UPDATERECORD
                        BitRecordTable(Index) = Record
                        function = ubound(BitRecordTable)
            
                    case %ADDRECORD
                        redim BitRecordTable(ubound(BitRecordTable)+1)
                        BitRecordTable(ubound(BitRecordTable)) = Record
                        function = ubound(BitRecordTable)
            
                    case %DELETERECORD
                        rem set delete flags in record
                        function = ubound(BitRecordTable)
            
                    case %RESETTABLE
                        redim BitRecord(-1)
            
                    case %GETPOINTER
                        function = varptr(BitRecordTable(0))
            
                    case %GETTABLESIZE
                        function = ubound(BitRecordTable)
            
            
                end select
            end function
            When opening a database and selecting a well to work on, all the
            records for that well will be "cached" in each function/table handler.

            Code:
            BitRecords = BitRecord(%ADDRECORD,0,NewBitRecord)

            Hope this makes sense...


            ------------------
            Cheers

            Comment


            • #7
              Hi Mark

              by re-entrant Tom means thread-safe, meaning that your dll
              must operate in single-threaded mode or that the function
              is synchronised.

              A function is re-entrant when it can be called from several
              threads. Using static variables makes your function non re-entrant.

              Otherwise one thread could overwrite the content of the static
              variable and you wouldn't get the results you are expecting.

              Cheers

              Florent



              ------------------

              Comment


              • #8
                Yes but not only threads.
                If you use a static in your wndproc...

                But i hadn't think about this problem yet, good to rethink this.
                When i create include files, most of the times i make use of a custom callback with a custom parameter.
                If the user uses it for a strptr() it could go wrong..


                ------------------
                [email protected]
                hellobasic

                Comment


                • #9
                  STATIC variables are stored in the data segment, the same as GLOBAL variables.

                  So they do not move around in memory for the life of the program.

                  However, with dynamic strings, the string HANDLE stays in place, but the string data moves all over the place.

                  And Tom's and Florent's comments on re-entrancy are right on the mark. STATIC variables are "global" to all instances of threads.

                  --Dave


                  ------------------
                  Home of the BASIC Gurus
                  www.basicguru.com
                  Home of the BASIC Gurus
                  www.basicguru.com

                  Comment


                  • #10
                    Whist all numeric, UDT and UNION class variables (scalar variables) do not move around in memory, this is not true for arrays.

                    PowerBASIC reserves the right to move any array (including Global and Static arrays) if it is REDIM'ed.

                    Dynamic string handles (as accessed with VARPTR()) also move if they are part of a dynamic string array (that is REDIM'ed). The contents of a dynamic string array (as accessed with STRPTR()) move with every assignment statement.

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment

                    Working...
                    X