UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on February 15, 2011, 09:27:51 AM

Title: Dynamic Array Allocation
Post by: kevin on February 15, 2011, 09:27:51 AM
 Dynamic Array Allocation


    This example shows how we can return allocated arrays from functions.  While an abstract concept, you can use this type of thing to manage blocks of data in arrays.  You can do the same thing with banks, but whatever floats your boat.

    The demo creates and display the constents to 10 dynamically created arrays..



[pbcode]

   // Dim an array to hold our dynamically allocated arrays
   Dim Handles(10)

   // Run through and make a bunch fo arrays
   For lp =1 to 10
      Handles(lp)=Make1DArray(rndrange(10,20))
   next
   

   // define an array stub,  which we'll use to look
   // at our allocated arrays
   MakeArray Temp()

   
   For lp =1 to 10
      // Get the array handle and assign to the Temp stub
      // now Temp() points at this array
      Temp()=Handles(lp)

      // run through and make a string of all the values
      // in this array
      s$=""
      for slp=1 to getarrayelements(temp())
        s$+=str$(temp(slp))+","
      next

      // print out the contents of this array
      print trimright$(s$,",")

   next

   Sync
   waitkey
   


// Make a 1D array of random size and fill it full of
// values from 0 - 1000
Function Make1DArray(size)
   Dim Me(size)
   For lp=0 to size
      Me(lp)=rnd(1000)   
   next
EndFunction Me()

[/pbcode]