Announcement

Collapse
No announcement yet.

Ubound of a Index Pointer

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

  • Ubound of a Index Pointer

    How do I find the ubound of an index pointer without passing it
    to a function?

    eg.
    Code:
    function example(address as dword) export as single
    ArrayPTR = Address
    Ubound& = <--------------------- How do I get this?
       for next a = 0 to ubound&
       ...
       ...
    
       next
    end function
    
    function pbmain() as long
       a! = example(VARPTR(ArrayIn(0))
    end function
    I know I could simple pass the ubound to the function as well or
    have it global but I really don't want to do that. Any ideas?

    Thanks

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    I prefer descriptors
    Code:
       #Compile Exe
       Function Bounds (Ar() As Long) As Long
          MsgBox Format$(LBound(Ar()), "LBOUND = #  ") + $CRLF + _
                 Format$(UBound(Ar()), "UBOUND = #  ") + $CRLF + _
                 Format$(VarPtr(Ar(LBound(Ar()))), "At = #")
       End Function
       Function PbMain
          Dim A(5 To 25) As Long
          Bounds A()
       End Function
    ------------------

    Comment


    • #3
      I know I could simple pass the ubound to the function as well or
      have it global but I really don't want to do that. Any ideas?
      When you pass an array by it's descriptor (as Semen's example shows), PowerBASIC internally passes a single 32-bit pointer to the array's descriptor table - very little overhead, and your Sub/Function code can have complete control over the array. This is likely to be the most efficient method. It is possible to access the information by reading the descriptor table values, but these tables are intentionally undocumented as they may change from version to version of PowerBASIC.

      Regardless, I don't see why is it so important to avoid using the methods that PowerBASIC currently provides to work with arrays in Subs/Functions. Maybe I'm missing something...?

      As an aside, it is likely that a future version of PowerBASIC will include facilities to programmatically query array descriptor information, but that will likely require access to the array descriptor too.



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

      Comment


      • #4
        Lance,

        I guess I'm just wanting to use pointers because I assume it's
        faster. The main reason I'm just simply wanting to pass
        the dword address to the function is so its easier to call from
        VB.

        I don't really understand Semen's example. To me it looks like
        common sense but you talk about descriptors?

        I'm just wanting to make one function that I can call with
        1 parameter that is a long or dword that will point to the
        address of the array and I wish to find the ubound of that
        pointer.



        ------------------
        -Greg
        -Greg
        [email protected]
        MCP,MCSA,MCSE,MCSD

        Comment


        • #5
          Greg --
          Probably, this code explains you better, how PB (and not only it) passes arrays.
          Each array (like a string, for example), has a descriptor, which describes dimensions, location and so on.
          When you use A(), PB simply transfers VarPtr to descriptor, which already exist before CALL.
          So for performance this is equal to transfer scalar variable.

          To demonstrate this, I included "hooligan action" and as you can see, LBound eats new "information".

          Code:
             #Compile Exe
             ' DO NOT USE SUCH WAY
             Function Bounds1 (Ar() As Long) As Long
                ReDim Arr(0) As Dword At VarPtr(Ar())
                MsgBox Format$(Arr(6), "LBOUND = # ") + $CRLF + _
                       Format$(Arr(7), "UBOUND = # ") + $CRLF + _
                       Format$(Arr(0), "At = #"), , "Variant 1"
                Arr(6) = 1752 ' hooligan action
             End Function
             ' USE THIS WAY
             Function Bounds2 (Ar() As Long) As Long
                MsgBox Format$(LBound(Ar()), "LBOUND = # ") + $CRLF + _
                       Format$(UBound(Ar()), "UBOUND = # ") + $CRLF + _
                       Format$(VarPtr(Ar(LBound(Ar()))), "At = #"), , "Variant 2"
             End Function
             Function PbMain
                Dim A(705 To 1425) As Long
                Bounds1 A()
                Bounds2 A()
             End Function
          [This message has been edited by Semen Matusovski (edited July 29, 2000).]

          Comment


          • #6
            I'm just wanting to make one function that I can call with
            1 parameter that is a long or dword that will point to the
            address of the array and I wish to find the ubound of that
            pointer.
            Let me spare you some angst: this cannot be done with the current release of the PowerBASIC Windows compilers.
            (That is, in a factory-supported manner).

            It can be done with the PB/DOS compilers using the internal functions ArrayCalc and ArrayInfo.

            MCM


            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              I posted an example to use the array descriptor.

              All that fear of incompatibility!
              If it changes, you simply adapt to that compiler ain't it?
              We are programmer's
              This looks much of the same fear of using callbacks in VB.
              Spooookkyyy!

              On my site in the freetips help, you can find a copy of that source.


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

              Comment

              Working...
              X