Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

String Array quickie

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

  • String Array quickie

    I'm looking for smaller-scale dynamic-string allocation.

    Arrays of strings are just contiguous arrays of pb string handles, right?

    local S$(), pS as string ptr
    dim S$(14)
    pS = varptr( S$) 'NOT STRPTR!
    @pS[1] = "This is string 1."
    @pS[2] = "This is string 2."
    @pS[3] = "This is string 3."
    'etc...

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

  • #2
    Should be pS = VarPtr(S(0))

    Code:
       #Compile Exe
       %n = 100000
       Function PbMain
         Local s() As String, pS As String Ptr, i As Long
    
         Dim S(%n)
         pS = VarPtr(S(0))
    
         For i = 0 To %n
            @pS[i] = "This is string" + Str$(i)
         Next
         
         ' Test
         For i = 0 To %n
            If S(i) <> "This is string" + Str$(i) Then MsgBox "Oh"
         Next
         
         MsgBox "Finish"
         
      End Function
    If you want to imitate, how PB keeps strings (without Dim S() As String), I think that it's possible and more or less easy, something like this
    (not optimal code, just idea only)

    Code:
       #Compile Exe
       %n = 10 ' no. of elements
       %sz = 1000000 ' maximum size (total)
       
       Function PbMain
         Local s As String, tmp As Asciiz * 81, pS As String Ptr
         Dim i As Long, js As Long, k As Long, kk As Long
         
         s = Space$(%sz)
         
         pS = StrPtr(S)
         js = 4 * %n
         For i = 0 To (%n - 1)
            tmp = "This is string" + Str$(i): kk = Len(Tmp)
            
            k = 4 * (Fix(kk / 4) + 2)
            Mid$(s, 4 * i + 1, 4) = Mkl$(ps + js + 4)
            Mid$(s$, js + 1, kk + 5) = Mkl$(kk) + Tmp + Chr$(0)
            js = js + k
         Next
         
         For i = 0 To (%n - 1)
            If @pS[i] <> "This is string" + Str$(i) Then MsgBox Str$(Len(@ps[i]))
         Next
    
         MsgBox "Finish"
    
      End Function
    [This message has been edited by Semen Matusovski (edited December 12, 2000).]

    Comment


    • #3
      Semen,

      In your simulation of PB string storage, why are the strings stored
      on 4 byte boundaries?

      It seems more efficient to simply let k = kk + 5,
      rather than k = 4 * (Fix(kk / 4) + 2).

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

      Comment


      • #4
        DWORD alignment is the most efficient way of storing most kinds of data in Win32, as the compiled code can be optimized for memory access in this way.

        For example, making fixed-length & ASCIIZ string and string members of UDT's a multiple of 4 bytes is the most efficient, as is the internal alignment of a UDT structure too.


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

        Comment

        Working...
        X