Results 1 to 1 of 1

Thread: Simple dynamic UDT-subarrays

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170

    dynamic UDT-subarrays for dummies

    This is a very simple tutorial script at beginner level

    How to append different length dynamic subarrays to some udt
    using heap-memory and virtual variables or layovers.


    It is meant for fixed size-elements of any type - so no dynamic string-arrays.
    (but with a little effort you can create a DWord-array to store pointers for each "string")

    This example stores some different sized arrays of Long-variables and shows how to access them again.
    To clearify that this Long is just a placeholder for any fixed-size variable or udt it's aliased as "someType"

    uses only built-in functions but needs 1.9.8 for the Redim-fix on virtual variables.

    #MINVERSION 1.9.8.0
    Uses "Console"
     
    ' this just to make the example better readeable:
    Alias Long As someType
     
     
    Type t_Type           ' whatever udt, don't care
                          ' it's just an example-type
      
    ' myArray() As someType is sadly not possible
    ' but we can help it...
     
      pArray      As DWord ' here node a dynamic array of some type
                           ' "pArray As DWord" here equals pointer of "myArray() As someType" 
                           
    End Type
     
    Dim foo(3) As t_Type  ' have an array of t_Type...
                          ' but ".myArray()" shall be something different to each,
                          ' maybe an array of someType in this example
                          ' each different number of elements
       
    Function TBMain()
      Dim i, j   As Long  ' some counters
       
      PrintL $CRLF & "different length sub-arrays example:" & $CRLF
      
    ' -- creating the data:
       
    ' a virtual array  As datatype  At (    now allocated space       ) and fill in data
     
      Local myArray(5) As someType  At HEAP_Alloc(5 * SizeOf(someType)) Value = 1,2,3,4,5 
       
    ' store pointer in foo(1).pArray using GetAt
     
      foo(1).pArray = GetAt(myArray)
     
    ' done with foo(1)...  
       
     
    ' now re-use myArray() for the next members of foo and  
     
    ' redim it new size At now allocated space...
     
      ReDim myArray(7)  At HEAP_Alloc(7* SizeOf(someType)) 
         
       
    ' VarPtr to FIRST element of myArray equals GetAT
         
      foo(2).pArray = VarPtr(myArray(1))  
       
       
    ' fill in some data
       
      Array Assign myArray = -1,-2,-3,-4,-5,-6,-7
       
     
       
    ' finally do the same to foo(3).pArray but another way:
        
    ' store pointer of allocated space for 8 elements of someType
     
      foo(3).pArray = HEAP_Alloc(8 * SizeOf(someType))
       
    ' Redim myArray(8) At allocated space 
     
      ReDim myArray(8) At foo(3).pArray
       
    ' fill in members data one by one this time:
     
      For i = 1 To UBound(myArray)
        myArray(i) = i * i
      Next 
       
     
    ' -- retrieving the data:
       
    ' go through all of foo()       
     
      For i = 1 To UBound(foo) 
       
        PrintL "foo(" & i & ").myArray :"
         
    ' place virtual array in correct size upon allocated heap:
     
        ReDim myArray(HEAP_Size(foo(i).pArray)/SizeOf(someType)) At foo(i).pArray   
         
    ' now can access it:
     
        PrintL Str$(UBound(myArray)) & " elements:"
         
    ' print out each array-member:
     
        For j = 1 To UBound(myArray)
          Print myArray(j) & $TAB
        Next       
         
        PrintL $CRLF & "--------------------------" & $CRLF
      Next
     
       
        PrintL "-------------------------> key to continue"
      WaitKey
      PrintL
    
    
    ' now how is this dynamic?
     
    ' want to add 1 more element to foo(1)-array...
     
      foo(1).pArray = HEAP_Realloc(foo(1).pArray, HEAP_Size(foo(1).pArray) + SizeOf(someType) * 1 )
     
    'check:
      ReDim myArray(HEAP_Size(foo(1).pArray)/SizeOf(someType)) At foo(1).pArray   
     
    ' set new value to last element of "foo(1).myArray()"
      myArray(UBound(myArray)) = 123
     
    ' see if the old ones are still there:
      For j = 1 To UBound(myArray)
        Print myArray(j) & $TAB
      Next      
      PrintL
       
      PrintL $CRLF & "-------------------------> key to end"
      WaitKey
     
    End Function
     
    ' allocated heap gets freed by thinBasic if script ends
    
    Last edited by ReneMiner; 31-10-2014 at 21:30.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Redim non-dynamic array?
    By ReneMiner in forum thinBasic General
    Replies: 3
    Last Post: 11-05-2013, 00:05
  2. Dynamic desktop background changer
    By Johannes in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 3
    Last Post: 14-11-2010, 22:33
  3. Oh wow, got it working, dynamic textures
    By kryton9 in forum TBGL General
    Replies: 16
    Last Post: 22-06-2007, 01:45

Members who have read this thread: 1

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
  •