Page 2 of 7 FirstFirst 1234 ... LastLast
Results 11 to 20 of 67

Thread: Release the beast: Get$/Set$/GetPtr

  1. #11
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    I see.

    I had to remove the older version since I discovered that ByRef passed UDT-subsets seem not to work, so there are some new functions and also some new syntax to some existing functions. Now these functions await an address where to find/store the pointer - these functions names end with "At" now.
    StrLen-Function still inside - attention that works only on String/String-Array but not on strings which are subsets of an UDT.

    Also Set$ I changed to SetStr since it does not return a string. Same applies to SetUdtStr. Mainly the functions work still the same way as described above, a few have been exchanged:

    Some demonstration code that shows usage of heap as dynamic UDT-subsets
    Heap_SetAt
    Heap_AppendAt

    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
     
    Type t_type
      S1 As DWord
      S2 As DWord
    End Type
    
    Type t_Subtype
      A As Long
      B As Byte
      C As Double
    End Type
    
    Dim foo(3) As t_type  
    Dim dummy As t_Subtype
    Dim i As Long
    
    For i = 1 To 3
     ' now tell foo(2).S1 to hold something:
    
      dummy.A += 12
      dummy.B += 3
      dummy.C += 4.5
      Heap_AppendAt(VarPtr(foo(2).S1), Memory_Get(VarPtr(dummy), SizeOf(t_Subtype)) )
      
    Next i
    ' retrieve data:
    Dim dArray(HEAP_Size(foo(2).S1)\SizeOf(t_SubType)) As t_SubType At foo(2).S1
    
    For i = 1 To UBound(dArray) 
     ' PrintL "element" + Str$(i)
      PrintL "A:" + dArray(i).A
      PrintL "B:" + dArray(i).B
      PrintL "C:" + dArray(i).C
      PrintL
    Next 
    
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    dummy.A = 77
    dummy.B = 77
    dummy.C = 77
    
    PrintL $CRLF + "now insert new element 2"
    
    Heap_ElementInsertAt(VarPtr(foo(2).S1), 2, Memory_Get(VarPtr(dummy), SizeOf(t_Subtype)) ) 
    
    ReDim dArray(HEAP_Size(foo(2).S1)\SizeOf(t_SubType)) As t_SubType At foo(2).S1
    
    For i = 1 To UBound(dArray) 
     ' PrintL "element" + Str$(i)
      PrintL "A:" + dArray(i).A
      PrintL "B:" + dArray(i).B
      PrintL "C:" + dArray(i).C
      PrintL
    Next 
     
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
     
    PrintL "new use of Heap_SetAt"
    
    Heap_SetAt VarPtr(foo(3).S2), "this is important"
    PrintL Heap_Get$(foo(3).S2)
    ' - - - - - - 
    String fish
    PrintL $CRLF + "new : SetStr (replaces Set$ since does not return string)"
    SetStr "fish", "apple juice"
    PrintL Get$("fish")
    PrintL 
    Dword myPtr = GetPtr "fish"
    PrintL StrAtPtr$ myPtr
    PrintL
    PrintL fish
      
    PrintL $CRLF + "key to end" + $CRLF
    WaitKey
    
    EDIT:
    Functions-Overview version 0.42 - find a link to the latest version there

    This attachements is an older version to run the posted examples before 12-08-2013.
    MinVersion 1.9.7.0
    Functions-Overview version 0.35
    FileVersion 0.35, 07-08-2013
    Attached Files Attached Files
    Last edited by ReneMiner; 13-08-2013 at 16:05.
    I think there are missing some Forum-sections as beta-testing and support

  2. #12
    sys and long are the same in a 32 bit system. Using sys for most integer variables is a safe bet.

  3. #13
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    I have 64Bit...but anyhow I try- I don't get Function thinBasic_ArrayGetPtr nor thinBasic_ArrayGetInfo to deliver results without crashing. Would be nice if one would not need to pass an elements size and the GetAnyPtr function would find out.
    Last edited by ReneMiner; 23-07-2013 at 15:25.
    I think there are missing some Forum-sections as beta-testing and support

  4. #14
    To clarify: I should have said 32 bit process The width of sys is determined by the compilation mode: 32 or 64 bit.

  5. #15
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    One more example how to use and organize Heap for storing dynamic arbitrary amounts of "sub-heap" and once more some "ByName"-example.

    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
    
    DWord foo  ' might be any subset of udt...
    Long i
    
    ' storing dynamic multiple subsets, let's say 7 Sub-Heaps to foo
    ' so make space for 7 pointers and save their positions to heap at foo
    
    foo = HEAP_Alloc( 7* SizeOf(DWord) )
    
    For i = 1 To 7
      PrintL "fill in data: 'I am Number"+Str$(i)+"'"  
      ' pass the position where to find/store the pointer:
      Heap_SetAt(foo + (i-1) * SizeOf(DWord), "I am number" + Str$(i) )
    Next  
    
    
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    ' all noded to one Dword, however:
    
    For i = 1 To HEAP_Size(foo)\SizeOf(DWord)
      PrintL Heap_Get$(Peek( DWord, foo + (i-1) * SizeOf(DWord)))
    Next
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    ' exchange number 3, access the single subitem only, pass the position(!) of the pointer:
    Heap_SetAt(foo + (3 - 1)* SizeOf(DWord), "hey - I am new number 3 here")
    
    ' or append one - have to refer to the parent here
    ' doing a few things in one step : create some new heap and append its pointer to heap at foo
    Heap_AppendAt VarPtr(foo), MKDWD$(HEAP_AllocByStr("so I should be the last"))
    
    ' squeeze new one at position 8 in:
    
    Heap_ElementInsertAt VarPtr(foo), 8,  MKDWD$(HEAP_AllocByStr("I sneak in here now"))
    ' also here: the result of Heap_AllocByStr gets inserted to the data found at the position
    ' stored at varptr foo, new resulting heap gets created and replaces the "old" one. Finally
    ' the new heaps address gets stored at varptr foo
    
    For i = 1 To HEAP_Size(foo)\SizeOf(DWord)
      PrintL Heap_Get$(Peek( DWord, foo + (i-1) * SizeOf(DWord)))
     
      ' can release heap using for example Heap_ResizeAt with a size of 0 
      HEAP_ResizeAt( foo + (i-1) * SizeOf(DWord), 0 ) 
     ' also valid - does basically the same:
     ' HEAP_SetAt( foo + (i-1) * SizeOf(DWord), "" ) 
    
    Next
    
    ' remember:
    HEAP_ResizeAt(VarPtr(foo), 0)
    ' equals 
    ' Free_Heap(foo) : foo = 0
    
    ' every of these subheaps could hold an array of any fixed size Type as "huge" as in the post above 
    ' you can "node" heaps to any of these - as many as your memory allows   
    
    PrintL  "-----------------------------------------" + $CRLF
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey                     
    
    ' just because it's so cool:
    String fishsoup
    ' changing string by passing its name:
    SetStr "fishsoup", "lemon juice" 
    ' retrieve Ptr by passing name
    foo = GetPtr "fishsoup"    
    ' retrieve string by passing its pointer
    PrintL StrAtPtr$ foo  
    ' or just as
    PrintL Get$ "fishsoup"
    ' returns same as
    PrintL "lemon juice"
    ' or
    PrintL fishsoup
    
    PrintL  "-----------------------------------------" + $CRLF
    'PrintL $CRLF + "key to continue" + $CRLF
    'WaitKey
    
    PrintL $CRLF + "key to end" + $CRLF 
    WaitKey
    
    you'll need the attachement "LazyFun.tBasicU" above or below saved next to this
    Last edited by ReneMiner; 07-08-2013 at 09:28.
    I think there are missing some Forum-sections as beta-testing and support

  6. #16
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Off topic

    Rene, I just wanted to say that while I have not posted anything for some time. I have been reading the forums on and off again and have enjoyed seeing an enthusiastic and prolific programmer contributing to this fine forum and language.

  7. #17
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    thanks guys. your words make me feel good - and I would not play around with this stuff if I wouldn't have some fun doing it. Once again I exchanged the attachement.
    I mostly went over it and filled it with a few more comments and a little explanation. A "little change" to Heap_Mid$-Function, see below on this post

    A small overview about the included functions:
    "Lazy Functions" - old version: find the new list here

    contains:

    --------------------------------------------------------------------------------
    functions to manipulate variables
    passed "byName" in a string - find an example covering these in code below
    i think these functions names will be prefixed Variable_ in near future...

    Get$ Get String-data from any variable passed "byName"
    GetDataPtr Get Pointer to Data of a certain element of any variable
    GetPtr Get Pointer to any variable
    SetStr Set String-data to any variable
    SetUdtStr Set String-data to dynamic-string in udts
    VarInfo - Alias thinBasic_VariableGetInfoEx, Lib "thinCore.dll"

    restrictions to these:
    for multidimensional arrays just the first elements pointer can be returned
    for multidimensional arrays only first or all elements capture is possible
    --------------------------------------------------------------------------------
    functions to manage Heap-memory

    >> common:
    Heap_AppendAt append some data to heap and get/store pointer at specified position
    Heap_Copy create copy of existing heap and return new pointer
    Heap_Fill fills/overwrites heap with some string-data
    Heap_Get$ get entire heap as string
    Heap_InsertAt insert new data at absolute position within heap
    Heap_Instr find the position of next occurence of string to find
    Heap_Left$ get left part/get string of desired length with heap content left
    Heap_Mid$ get mid part/get string of desired length and heap within
    Heap_ResizeAt resizes heap, preserving data, resize to 0 will just free the heap
    Heap_Right$ get right part/string of desired lenght with heap-content right
    Heap_SetAt set new data to heap and get/store pointer at the specified position

    >> elements stored in "1-dimensional, fixed size heap-array":
    Heap_ElementFreePtrAt removes a pointer-element and free sub-heap
    Heap_ElementGet$ returns data of one single element
    Heap_ElementGetPtr returns pointer to single element
    Heap_ElementIDAt returns index of data, creates it if not exists
    HEAP_ElementPeekPtr
    peek pointer that's stored as element - NOT INCLUDED YET because of bigger news to await about Heap_Nodes
    Heap_ElementInsertAt
    insert data as new element
    Heap_ElementRemoveAt will remove one element
    Heap_ElementSet
    set data to an existing element

    >> organized in multidimensional storage:
    Heap_OrganizeAddress will return an address where to find/store data
    Heap_OrganizeAt will create organized heap with at least 2 dimensions
    Heap_OrganizerFreeAt will free heap + organized sub-heap
    Heap_OrganizerGetDim$ returns information about organized heap dimensions

    remarks:
    all Heap-functions ending ...At await a position where to store or find a pointer so all these create some new heap and/or take care of releasing the old data and update the passed position with the new pointer or 0 if released.

    --------------------------------------------------------------------------------
    functions to dynamic strings

    Dictionary_Len returns size of dictionary-buckets
    StrAtPtr$ returns string found at passed position
    StrLen - limited usage since not works on udt-substrings, shortens StrPtrLen(StrPtr())

    --------------------------------------------------------------------------------
    pointer functions

    PeekPtrAt safe way to peek pointers without risk to Peek at 0



    all functions ending with $ return string-data- however
    #MINVERSION 1.9.7.0
    find the attachement above or at the recent posts in this thread

    Here also once more an example-script about the in the title of this thread mentioned -and their related- functions as they work now:

    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
    
    Type t_type
      A As Long
      B As Byte
      C As String
    End Type
    Dim hut(3) As t_type
    
    String foo
    DWord myPtr, test
    Long i
    
    PrintL "change a simple variables content using SetStr" + $CRLF
    SetStr "foo", "I am new content of foo now"        
    PrintL foo
    
    PrintL Get$("foo")
    ' retrieve a pointer:
    myPtr = GetPtr("foo")
    ' in case String use StrAtPtr$
    PrintL StrAtPtr$(myPtr) 
    PrintL
    
    ' numeral:
    SetStr "test", MKDWD$(12345)
    PrintL "Value at test:" + Str$(test)
    
    myPtr = GetPtr("test")
    PrintL Peek(DWord, myPtr) 
    
    PrintL CVDWD(Get$("test",,SizeOf(DWord)))
    
    
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    PrintL "fill in some data to udt-array:" + $CRLF
    
    For i = 1 To UBound(hut)
      hut(i).A = 42 * i  
      PrintL "hut("+i+").A:" + Str$(hut(i).A)
      
      hut(i).B = i
      PrintL "hut("+i+").B:" + Str$(hut(i).B)
      hut(i).C = "I am hut("+i+").C"
      PrintL "hut("+i+").C: " + hut(i).C
      PrintL
    Next
    
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    PrintL "retrieve data of one complete array-element: hut(2)"+ $CRLF
    PrintL "using Get$"
    foo = Get$("hut", 2, SizeOf(t_Type))
    
    PrintL "A: " + Peek(Long, StrPtr(foo))
    PrintL "B: " + Peek(Byte, StrPtr(foo) + SizeOf(Long)) 
    
    '              StrAtPtr$ will return the string stored at passed Pointer
    PrintL "C: " + StrAtPtr$(PeekPtrAt StrPtr(foo) + UDT_ElementOffset(hut(1).C))
    
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    PrintL "retrieve data of dynamic udt sub-string hut(3).C" + $CRLF
    PrintL "using GetDataPtr" 
    
    myPtr = GetDataPtr("hut", 3, UDT_ElementOffset(hut(1).C), SizeOf(t_Type) ) 
    
    Print "hut(3).C: " ' in case udt-substring have to peek the pointer at myPtr:
    PrintL StrAtPtr$(PeekPtrAt myPtr)
    
    ' in other cases peek the value directly at myPtr
    myPtr = GetDataPtr("hut", 3, UDT_ElementOffset(hut(1).A), SizeOf(t_Type) ) 
    PrintL "hut(3).A:" + Str$(Peek(Long, myPtr))
    
    PrintL $CRLF + "change data of dynamic udt sub-string:" + $CRLF
    PrintL "using SetUdtStr"
    SetUdtStr "hut", 3, UDT_ElementOffset(hut(1).C), SizeOf(t_Type), "Now I have been changed"
    
    PrintL $CRLF + "re-check: " + hut(3).C
    ' make sure it's correct here too:
    myPtr = GetDataPtr("hut", 3, UDT_ElementOffset(hut(1).C), SizeOf(t_Type) )
    PrintL StrAtPtr$(PeekPtrAt myPtr)
    
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to end" + $CRLF 
    WaitKey
    
    So there's just to say, GetPtr-Function will return immediately the correct pointer - either VarPtr or StrPtr while GetDataPtr always returns pointer to the data, which is in case string... a stringpointer

    Small change to Heap_Mid$-Function to match the functionalities of Heap_Left$/Heap_Right$ so this takes now an optional Byte-parameter to fill missing bytes with its value
    Also accepts negative start-value now to append bytes in front, if passed Len exceeds Heap in any way the rest will be filled with the passed bFill-byte or 0 by default

    usage example:
    Uses "console"
    #INCLUDE "LazyFun.tBasicU"
    
    
    DWord foo = HEAP_AllocByStr(Repeat$(4,"123456789|"))
     
    ' test get entire heap
    PrintL Heap_Mid$(foo, 1, HEAP_Size(foo))
     
    ' get 40 bytes, thus append left 10 spaces and use 30 from heap
    PrintL Heap_Mid$(foo, -10, 40, Asc(" "))
          
    ' get 40 bytes, start at byte 11, use heap till end and fill desired len
    PrintL Heap_Mid$(foo, 11, 40, Asc("."))       
     
    ' get 50 bytes, even if Heap is smaller, start to fill 5 bytes in front 
    PrintL Heap_Mid$(foo, -5, 50, Asc("+"))
     
    Heap_SetAt VarPtr foo, "" ' free the Heap and update the pointer stored in foo
    
    PrintL "----+----|----+----|----+----|----+----|----+----|"
    PrintL $CRLF + "any key to end" + $CRLF
    
    WaitKey
    
    Last edited by ReneMiner; 12-08-2013 at 12:32.
    I think there are missing some Forum-sections as beta-testing and support

  8. #18
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    An additional example - use the attachement above :

    new function

    Heap_ElementRemoveAt
    which allows to remove an element inside a row of even sized elements stored to heap-memory. As the name tells it awaits a position where to find/save the pointer.
    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
     
    Type t_type
      S1 As DWord
      S2 As DWord
    End Type
    
    Type t_Subtype
      A As Long
      B As Byte
      C As Double
    End Type
    
    Dim foo(3) As t_type  
    Dim dummy As t_Subtype
    Dim i As Long
    
    For i = 1 To 3
     ' now tell foo(2).S1 to hold something:
    
      dummy.A += 12
      dummy.B += 3
      dummy.C += 4.5
      Heap_AppendAt(VarPtr(foo(2).S1), Memory_Get(VarPtr(dummy), SizeOf(t_Subtype)) )
      
    Next i
    ' retrieve data:
    Dim dArray(HEAP_Size(foo(2).S1)\SizeOf(t_SubType)) As t_SubType At foo(2).S1
    
    For i = 1 To UBound(dArray) 
     ' PrintL "element" + Str$(i)
      PrintL "A:" + dArray(i).A
      PrintL "B:" + dArray(i).B
      PrintL "C:" + dArray(i).C
      PrintL
    Next 
    
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey 
    
    ' create a copy at foo(3).S1
    foo(3).S1 = Heap_Copy(foo(2).S1)
    dummy.A = 77
    dummy.B = 77
    dummy.C = 77
    
    PrintL $CRLF + "now insert new element 2"
    
    Heap_ElementInsertAt(VarPtr(foo(3).S1), 2, Memory_Get(VarPtr(dummy), SizeOf(t_Subtype)) ) 
    
    ReDim dArray(HEAP_Size(foo(3).S1)\SizeOf(t_SubType)) At foo(3).S1
    
    For i = 1 To UBound(dArray) 
     ' PrintL "element" + Str$(i)
      PrintL "A:" + dArray(i).A
      PrintL "B:" + dArray(i).B
      PrintL "C:" + dArray(i).C
      PrintL
    Next 
     
    PrintL $CRLF + "key to continue" + $CRLF
    WaitKey
    
    PrintL $CRLF + "now remove element 2"
    Heap_ElementRemoveAt VarPtr(foo(3).S1), 2, SizeOf(t_Subtype)
     
    ReDim dArray(HEAP_Size(foo(3).S1)\SizeOf(t_SubType)) At foo(3).S1
    
    For i = 1 To UBound(dArray) 
     ' PrintL "element" + Str$(i)
      PrintL "A:" + dArray(i).A
      PrintL "B:" + dArray(i).B
      PrintL "C:" + dArray(i).C
      PrintL
    Next 
    
    PrintL $CRLF + "key to end" + $CRLF    
    WaitKey
    
    Last edited by ReneMiner; 02-08-2013 at 06:50.
    I think there are missing some Forum-sections as beta-testing and support

  9. #19
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    Store data at heap in up to 8 dimensions now - accessible through one single pointer
    I think theres no problem to raise that to 31 dimensions... but I think 8 dimensions should serve the most usual needs. If it happens to you that you are in need of 42 dimensions then the call has to be changed slightly to pass all these parameters.

    Now there are additional:

    Heap_OrganizeAt
    Heap_OrganizeAddress
    Heap_OrganizerFreeAt

    Heap_OrganizerGetDim$

    One script-example to this:
    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
     
    Type t_Type
      A As Long
      B As Byte
      C As DWord
    End Type
                                   
    DWord foo    ' here we node heap
    DWord myPtr
    Dim dummy As t_Type At 0  ' virtual "peekhole"
     
    ' setup the dimensions in following way:
    ' fixed-size way:                                     
                 ' somewhat like "Dim Heap foo(5,4,3,5) as t_Type"
                 ' currently maximum of 8 dimensions possible
    If Heap_OrganizeAt( VarPtr(foo), MKDWD$(5, 4, 3, 5), SizeOf(t_Type) ) Then
      ' this call will setup TWO heaps now: 
      ' one as the organizer which holds pointer to data-heap and 
      ' information about dimensions - this one is quite small, depending
      ' on count of dimensions 4 Byte per dimension + a pointer + size, 
      ' ( in this example here 24 Byte )
      ' and an additional heap wich will store the actual data. It depends
      ' on dimensions and usage, can become quite large...
      ' the first dword at position of "organizer"-heap(foo) will hold the pointer to the
      ' data-heap then, the second dword is the size of one element if fixed - or 0 if not
      ' the following dwords hold the dimensions in the passed order
     
       ' this call requests the pointer of element 1,1,1,1 in organized heap foo
      myPtr = Heap_OrganizeAddress( foo, 1, 1, 1, 1 )
       
      If myPtr Then
        SetAt(dummy, myPtr) ' put the dummy-pattern onto this position 
        dummy.A = 11      ' and put some values there
        dummy.B = 11
        dummy.C = 11
      Else
        PrintL "no pointer?"
      EndIf
       
      SetAt(dummy, Heap_OrganizeAddress( foo, 5, 4, 3, 5 ))
      dummy.A = 22      
      dummy.B = 22
      dummy.C = 22
       
      SetAt(dummy, Heap_OrganizeAddress( foo, 3, 3, 3, 3 ))
      dummy.A = 33      
      dummy.B = 33
      dummy.C = 33
       
      PrintL "Get back for 1,1,1,1"
      SetAt(dummy, Heap_OrganizeAddress( foo, 1, 1, 1, 1 ))
      PrintL dummy.A
      PrintL dummy.B
      PrintL dummy.C 
      PrintL
         
      PrintL "Get back for 5,4,3,5"
      SetAt(dummy, Heap_OrganizeAddress( foo, 5, 4, 3, 5 ))
      PrintL dummy.A
      PrintL dummy.B
      PrintL dummy.C 
      PrintL
      
      PrintL "Get back for 3,3,3,3"
      SetAt(dummy, Heap_OrganizeAddress( foo, 3, 3, 3, 3 ))
       
      PrintL dummy.A
      PrintL dummy.B
      PrintL dummy.C 
      PrintL $CRLF + "-----------------------------------------"
      PrintL $CRLF + "key to continue" + $CRLF  
      WaitKey
       
      ' remove the virtual "Peekhole" before killing memory: 
      SetAt(dummy, 0)
      
      Heap_OrganizerFreeAt VarPtr foo
       
      ' variable foo holds the position of where heap(foo) can be found
      ' which is the "organizer"
       
      ' at that position inside the organizer stored is the pointer
      ' to organized "data-heap" which has been released now too
       
        
    EndIf
     
    ' unfixed-size way:
    ' more "complicated" but dynamic in elements-size
    ' setup some new dimensions
     
    ' the element size in the organizer becomes SizeOf(Dword) automatic - do not pass a size here to stay unfixed!
    ' omit size or pass 0 to organize pointers where you can organize heap of different sizes in each element.
    ' so on "cleanup" using Heap_OrganizerFreeAt the function "knows" that there are probably pointers to 
    ' allocated heap which has to be deleted too
     
    If Heap_OrganizeAt( VarPtr(foo), MKDWD$(3,4,5) ) Then
    ' creates two heaps: one to store information and one
    ' supposed to store just pointers where your data can be found
      
      myPtr = Heap_OrganizeAddress(foo, 1, 2, 3)
    ' here this call requests the address where the pointer shall be stored 
    ' since we want to store a pointer to some heap here
     
    ' now put there some data to store:
       
      Heap_SetAt myPtr, "this is data 1 2 3"
      'which creates heap "this is data 1 2 3" and stores the pointer at myPtr
      PrintL Heap_Get$(PeekPtrAt myPtr)
    ' request pointer: - in case use it more than once it's better to save the
    ' result local instead of calculating the position again
      myPtr = Heap_OrganizeAddress(foo, 3, 4, 5)  
      Heap_SetAt myPtr, "and this is data 3 4 5" ' size does not matter...
      PrintL Heap_Get$(PeekPtrAt myPtr) 
    ' re-check:
      PrintL Heap_Get$( PeekPtrAt Heap_OrganizeAddress(foo, 1, 2, 3) )
    ' free resources:  
      Heap_OrganizerFreeAt(VarPtr(foo))
    ' the organizer will not delete any noded sub-organizers !
    ' but it will delete all heap wich was organized through
    ' foo here so the data is gone now! 
    ' Either free noded sub-organizers in advance - or they are lost  
     
      PrintL "-----------------------------------------"
      PrintL $CRLF + "key to continue" + $CRLF  
      WaitKey
      
    EndIf  
    ' next test, 2 dimensions, fixed size
     
    If Heap_OrganizeAt( VarPtr(foo), MKDWD$(77, 99), SizeOf(Double) ) Then
      ' fill in some data
      myPtr = Heap_OrganizeAddress(foo, 1,1 )
      If myPtr Then
        Poke(Double, myPtr, 1.1)
      Else
        PrintL "no pointer?"
      EndIf 
     
      Poke(Double, Heap_OrganizeAddress(foo, 2,2 ), 2.2)
      Poke(Double, Heap_OrganizeAddress(foo, 66,66 ), 66.66)
      Poke(Double, Heap_OrganizeAddress(foo, 77,77 ), 77.77)
       
      myPtr = Heap_OrganizeAddress(foo, 1,1 )
      If myPtr Then PrintL "re-check: 1,1: " + Peek(Double, myPtr)
       
      PrintL "re-check: 2,2: " + Peek(Double, Heap_OrganizeAddress(foo, 2,2 ))
      PrintL "re-check: 66,66: " + Peek(Double, Heap_OrganizeAddress(foo, 66,66 ))
      PrintL "re-check: 77,77: " + Peek(Double, Heap_OrganizeAddress(foo, 77,77 ))
     
      PrintL $CRLF + "-----------------------------------------"
      PrintL $CRLF + "key to continue" + $CRLF  
      WaitKey
     
      Heap_OrganizerFreeAt VarPtr foo
      
    EndIf           
     
    ' now it gets crazy: 5 Dimensions where elements can vary in size
    '============================================
    '        DON'T RUN THIS ON A WEAK SYSTEM !
    '============================================
     
    If Heap_OrganizeAt( VarPtr(foo), MKDWD$(12, 23, 34, 45, 56) ) Then
               '= 23647680 addresses...= 94 590 720 Bytes to store just the pointers...
      myPtr = Heap_OrganizeAddress(foo, 6,11,17,22,28)
      If myPtr Then
        Heap_SetAt myPtr, "this is data 6, 11, 17, 22, 28"
        PrintL Heap_Get$(PeekPtrAt myPtr)
      Else
        PrintL "no pointer?"
      EndIf 
       
      myPtr = Heap_OrganizeAddress(foo, 7,7,7,7,7)
      Heap_SetAt myPtr, "this is data 7,7,7,7,7"
      PrintL Heap_Get$(PeekPtrAt myPtr)
         
      PrintL "Re-check: " + Heap_Get$( PeekPtrAt Heap_OrganizeAddress(foo, 6,11,17,22,28) )
      PrintL "Re-check: " + Heap_Get$( PeekPtrAt Heap_OrganizeAddress(foo, 7,7,7,7,7) )
         
      ' next commented on purpose:
      ' Heap_OrganizerFreeAt VarPtr foo - needs very long- we're finished anyway  
    EndIf
    PrintL $CRLF + "-----------------------------------------"
    PrintL $CRLF + "key to end" + $CRLF
    WaitKey
    
    Don't forget- you can "append" such a thing to anything that has 4 Bytes


    another small - hopefully now very easy to understand one that shows how to use heap to store "multidimensional array of pointers"
    Uses "Console"
    #INCLUDE "lazyFun.tBasicU"
    
    ' very simple example 3-dimensions, arbitrary size of each sub-element
                                  
    DWord foo 
    DWord myPtr 
    Long i, j ,k
    
    ' setup the dimensions:
    
    If HEAP_OrganizeAt( VarPtr(foo), MKDWD$(2,3,4) ) Then
      For i = 1 To 2
        For j = 1 To 3
          For k = 1 To 4
            myPtr = Heap_OrganizeAddress( foo, i, j, k )
            If myPtr Then 
              Heap_SetAt myPtr, "i="+i+" j="+j+" k="+k
              PrintL "got there: " + Heap_Get$(PeekPtrAt myPtr)
            EndIf
          Next
        Next
      Next 
      
      PrintL
      PrintL "now get back the stuff:"
      For i = 1 To 2
        For j = 1 To 3
          For k = 1 To 4
            PrintL "Re-check: " + Heap_Get$( PeekPtrAt Heap_OrganizeAddress( foo, i, j, k ) )
          Next
        Next
      Next
      
      Heap_OrganizerFreeAt VarPtr foo  ' no real need here since script ends now...
       
    EndIf 
    PrintL "-----------------------------------------"
    PrintL $CRLF + "key to end" + $CRLF
    WaitKey
    
    no protest? Is it really that easy?
    Last edited by ReneMiner; 31-07-2013 at 14:45.
    I think there are missing some Forum-sections as beta-testing and support

  10. #20
    Member
    Join Date
    Nov 2012
    Location
    Missouri, USA
    Posts
    113
    Rep Power
    30
    Rene,

    Could you please number your programs when you update them?

    Bill

Page 2 of 7 FirstFirst 1234 ... LastLast

Similar Threads

  1. Release of Aung San Suu Kyi
    By Charles Pegge in forum Shout Box Area
    Replies: 0
    Last Post: 13-11-2010, 14:23
  2. TAB Alpha Release 35
    By catventure in forum T.A.B. (ThinBasic Adventure Builder)
    Replies: 4
    Last Post: 08-07-2008, 18:34
  3. TAB Alpha Release 24
    By catventure in forum T.A.B. (ThinBasic Adventure Builder)
    Replies: 2
    Last Post: 22-03-2007, 00:37
  4. TAB Alpha Release 23
    By catventure in forum T.A.B. (ThinBasic Adventure Builder)
    Replies: 46
    Last Post: 21-03-2007, 19:02
  5. TAB Alpha Release 22
    By catventure in forum T.A.B. (ThinBasic Adventure Builder)
    Replies: 24
    Last Post: 08-03-2007, 13:58

Members who have read this thread: 0

There are no members to list at the moment.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •