No announcement yet.

How does this work? (StrPtr)

  • Filter
  • Time
  • Show
Clear All
new posts

  • How does this work? (StrPtr)

    I discovered that String Ptr somehow maintains it's string length in the provided handle.
    How can this be?
    Where is it stored?
    Should i make precautions because i make different uses of the same heapalloc.
    I can't imagne that PB somewhere keeps a reference.

    A copy of 2 heapallocs 'copies' this same length indicator!

    #Compile Exe
    #Include ""
    Function PbMain
        Dim h1 As Long
        Dim h2 As Long
        Dim h3 As Long
        Dim T1 As String Ptr
        Dim T2 As String Ptr
        Dim T3 As String Ptr
        h1 = HeapAlloc( GetProcessHeap(), %HEAP_ZERO_MEMORY, 1000 )
        h2 = HeapAlloc( GetProcessHeap(), %HEAP_ZERO_MEMORY, 1000 )
        h3 = HeapAlloc( GetProcessHeap(), %HEAP_ZERO_MEMORY, 1000 )
        T1 = h1
        MsgBox "1> " & Str$( Len( @T1 ) )
        @T1 = Space$( 500 )
        MsgBox "2> " & Str$( Len( @T1 ) )
        T2 = h1
        MsgBox "3> " & Str$( Len( @T2 ) )
        MoveMemory h3, h1, 1000
        T3 = h3
        MsgBox "4> " & Str$( Len( @T3 ) )
        If h1 Then Call HeapFree( GetProcessHeap(), 0, ByVal h1 )
        If h2 Then Call HeapFree( GetProcessHeap(), 0, ByVal h2 )
        If h3 Then Call HeapFree( GetProcessHeap(), 0, ByVal h3 )
    End Function
    [email protected]

  • #2
    Edwin --
    From PB/DLL help:

    If you need to determine the current length of a dynamic string, there are two ways to do so.
    The end of string is always followed by a nul, CHR$(0), so it is possible to scan the string for the first occurrence.
    Of course, this will only work if there are no embedded nuls in the string data.

    An alternative method is to read the 32-bit long integer which immediately precedes the start of the string data, as the current length is always stored there.

    E-MAIL: [email protected]