Announcement

Collapse
No announcement yet.

Array's in Callback functions

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

  • Array's in Callback functions

    Dumb question, are Arrays STATIC in a callback function or not?

    I've seen some weird behvior with an ar ray of mine and just wondering if it's because it's not static.......?

    Tried to RTFM but didn't find what i was looking for.

    Tx,
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    They are if you declare them as static; otherwise don't. As any other variable, BTW.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Originally posted by Scott Turchin View Post
      Dumb question, are Arrays STATIC in a callback function or not?

      I've seen some weird behvior with an ar ray of mine and just wondering if it's because it's not static.......?
      I'm assuming that you declare the array inside the CALLBACK function. Does the weird behaviour stop if you declare it STATIC?

      Comment


      • #4
        Is this a case of not using #DIM ALL?
        Rod
        I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

        Comment


        • #5
          Originally posted by Rodney
          Is this a case of not using #DIM ALL?
          No. #DIM ALL would require an array be dimensioned before use, but in a callback the array must be declared STATIC if you want the values to be persistent while the program is running. This was Scot's problem.

          Jose already noted this as well as pointing out that any array declared inside of callback would have to be declared STATIC as well if you wan is value to be persistent during program operation when the callback is active.

          However if the array is being passed to the callback, it must be persistent, STATIC, where it is declared and dimensioned. Otherwise it goes out of scope and is erased. Even a GLOBAL array needs not just its declaration but it needs to be dimensioned before use as well.
          Rick Angell

          Comment


          • #6
            Originally posted by Richard Angell View Post
            However if the array is being passed to the callback, it must be persistent
            meaning GLOBAL, STATIC, or DIM AT, presumably.

            Comment


            • #7
              Thanks Richard,

              Got it. I was thinking though that he might have missed some element of the declaration because he didn't have the #DIM ALL and it wasn't being flagged by the compiler as a result. No idea what specifically.
              Rod
              I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

              Comment


              • #8
                Chris,

                DIM ... AT does not guarantee persistence. By persistence, we would mean the memory is allocated and the data valid for the access we are using. The help (DIM) notes this about absolute arrays, note that you can get a GPF if the underlying memory has been deallocated by the procedure that created it:
                Care must be exercised when using absolute arrays, since the contents of an absolute array can only be valid for the scope of the memory the array references. If an absolute array references memory that is LOCAL to the procedure, the array contents become invalidated if the target memory block is released. For example, by either explicitly deallocating the memory block, or exiting the procedure itself. Attempting to access absolute array memory that has been deallocated will likely trigger a General Protection Fault (GPF). On this basis, absolute arrays should be LOCAL to the procedure in which they are to be used.
                Rick Angell

                Comment


                • #9
                  The important point here is a very simple one:

                  An array in a Callback function is no different than any other array.

                  An array is an array, regardless of its location. The rules do not change.

                  Best regards,

                  Bob Zale
                  PowerBASIC Inc.

                  Comment


                  • #10
                    I just made it a global array...

                    I'll go back when time permits and test this - basically it was holding a thread handle for a thread, and when it was released that piece in the array could be used, thus limiting 250 connections by limiting how many threads could be opened...

                    This is a point where a global came in handy, but I'll go back and try.

                    Dim MyArray(1 to 250) as long

                    Static MyArray(1 to 250) as long blows up.

                    I need to RTFM on this standbye..
                    Scott Turchin
                    MCSE, MCP+I
                    http://www.tngbbs.com
                    ----------------------
                    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                    Comment


                    • #11
                      Code:
                      S    = STRING$ (250 * 4 ,0)  ' 4 = sizeof(Long)
                      REDIM L (255) AS LONG AT STRPTR(S) 
                      L(0)   = 19 
                      L (12) = 12345
                      ...
                      ' ie, you can pass an array as a string, as long as you 'REDIM AT' before using.

                      Easiest way:
                      Code:
                      FUNCTION WinMain
                      
                        LOCAL S AS STRING 
                      
                        DIALOG NEW.... 
                      
                         S = STRING$ (250 * 4, 0)    ' space for 250 LONG elements 
                         DIALOG SET USER hDlg, N, VARPTR(S)   ' VARPTR not STRPTR here 
                      
                      
                      CALLBACK FUNCTION ....
                       LOCAL PS AS STRING PTR , L() AS LONG
                      
                         ....
                             DIALOG GET USER CBHNDL, N TO pS 
                             REDIM   L (LEN(@ps)\4 -1) AT STRPTR (@pS) ' redim based on current string length. 
                             SHOW FORMAT$(L(12))
                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Dim hThread(1 to BBSDown.MaxThreads) as Static Long


                        That did the trick, I wrote a debug log during each request from the webserver and hammered it with 20 instant hits, and I saw

                        lLoop: 1, thread handle 218
                        lLoop: 2, thread handle 272 (or whatever0

                        etc through 20.

                        Thanks, question answered!
                        Scott Turchin
                        MCSE, MCP+I
                        http://www.tngbbs.com
                        ----------------------
                        True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                        Comment


                        • #13
                          Actually it brings up a question.
                          Is "Static" really a global, but kept within the function in its described? kind of thing.

                          On one hand, static is easier to read within the scope of your function (and especially debugging), on the other Global is accessed from all functions (but can cause trouble if one function changes the value of the variable, when the other function is working on it)

                          Probably a matter of choice (as its long been debated), but I just wondered the idea of WHY static is better???
                          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


                          • #14
                            >Is "Static" really a global, but kept within the function in its described?

                            Yes. The data are stored the same as GLOBAL variables, although at compile time the variable name is recognized only within its defining procedure.
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment

                            Working...
                            X