FreeBasic doesn't allow dybnamic arrays within a type definition. To get around that, we can create a dynamic array using a pointer. The following commented code illustrates creating and maintaining a pointer-based dynamic array.
/'****************************************************************************
*
* Name: d1array.bas
*
* Synopsis: Example of creating a 1-D dynamic array in a type def.
*
* Description: FreeBasic doesn't support dynamic arrays in type defs, however
* this can be accomplished using pointer arrays.
*
* Copyright 2009, Richard D. Clark
*
* The Wide Open License (WOL)
*
* Permission to use, copy, modify, distribute and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice and this license appear in all source copies.
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF
* ANY KIND. See http://www.dspguru.com/wol.htm for more information.
*
*****************************************************************************'/
'Define our Null.
#Define NULL 0
'1-D Dynamic array in type def using pointers.
Type onedarray
elcnt As Integer 'The number of elements in our array.
arrptr As Integer Ptr 'Let's create an integer array.
End Type
'Create a type variable.
Dim dyn1 As onedarray
'Just for some random data.
Randomize Timer
'The following code creates a 1-D array and then adjusts the array dynamically.
'Create 10 integers in our array. Callocate, like Allocate, creates some space for the array
'but also zeros out the space. Allocate just allocates some space, but the space may contain
'garbage data, so the space needs to be zeroed out before use.
dyn1.arrptr = Callocate(10 * SizeOf(Integer))
'Make sure we could allocate the space.
If dyn1.arrptr = NULL Then
Print "Could not allocate integer array."
Sleep
End
EndIf
'Set the number of elements. This is important because there is no way to know how many elements
'are in our array without keeping track of them ourselves.
dyn1.elcnt = 10
'Check to see that the data space was zeroed out. Pointer arrays are zero based so the element index
'will run from 0 to number_of_elements - 1.
Print "Let's make sure array was zeroed out."
Print
For i As Integer = 0 To dyn1.elcnt - 1
'We are going to use the subscript method of accessing the pointer since it looks like an array access
'and makes working with the pointer much like working with a standard array.
Print "Index: " & i & " Value: " & dyn1.arrptr[i]
Next
Sleep
Print
'Output:
'**********************************************************************************************************
' Let's make sure array was zeroed out.
' Index: 0 Value: 0
' Index: 1 Value: 0
' Index: 2 Value: 0
' Index: 3 Value: 0
' Index: 4 Value: 0
' Index: 5 Value: 0
' Index: 6 Value: 0
' Index: 7 Value: 0
' Index: 8 Value: 0
' Index: 9 Value: 0
'**********************************************************************************************************
'So far so good. Let's add some data to our array.
For i As Integer = 0 To dyn1.elcnt - 1
'Again we use the subscript method so that we can code this just like any old array.
dyn1.arrptr[i] = Rnd * 100
Next
Print "Added some random data to array."
Print
'Let's see what we got here.
For i As Integer = 0 To dyn1.elcnt - 1
Print "Index: " & i & " Value: " & dyn1.arrptr[i]
Next
Sleep
Print
'Output:
'**********************************************************************************************************
' Added some random data to array.
' Index: 0 Value: 12
' Index: 1 Value: 29
' Index: 2 Value: 15
' Index: 3 Value: 92
' Index: 4 Value: 11
' Index: 5 Value: 8
' Index: 6 Value: 52
' Index: 7 Value: 96
' Index: 8 Value: 34
' Index: 9 Value: 52
'**********************************************************************************************************
'Let's cut the array in half. This will mean we will lose the data from index 5 to the end of the
'current array. Reallocate will adjust the size of the pointer space.
dyn1.arrptr = ReAllocate(dyn1.arrptr, 5 * SizeOf(Integer))
'Make sure we have no errors.
If dyn1.arrptr = NULL Then
Print "Could not allocate integer array."
Sleep
End
EndIf
'We need to update the element count so we don't run off the array.
dyn1.elcnt = 5
Print "Truncated the array."
Print
'The previous data for elements 0 to 4 should be unchanged.
For i As Integer = 0 To dyn1.elcnt - 1
Print "Index: " & i & " Value: " & dyn1.arrptr[i]
Next
Sleep
Print
'Output:
'**********************************************************************************************************
' Truncated the array.
' Index: 0 Value: 12
' Index: 1 Value: 29
' Index: 2 Value: 15
' Index: 3 Value: 92
' Index: 4 Value: 11
'**********************************************************************************************************
'Let's add in 5 more elements to the array using Reallocate.
dyn1.arrptr = ReAllocate(dyn1.arrptr, 10 * SizeOf(Integer))
'Make sure we have no errors.
If dyn1.arrptr = NULL Then
Print "Could not allocate integer array."
Sleep
End
EndIf
'Always update the element count when changing the size of the array.
dyn1.elcnt = 10
'Let's see what we have now.
Print "Expanded the array."
Print
'The previous data for elements 0 to 4 should be unchanged. Elements 5+ will be uinitialzed.
For i As Integer = 0 To dyn1.elcnt - 1
Print "Index: " & i & " Value: " & dyn1.arrptr[i]
Next
Sleep
Print
'Output:
'**********************************************************************************************************
' Expanded the array.
' Index: 0 Value: 12
' Index: 1 Value: 29
' Index: 2 Value: 15
' Index: 3 Value: 92
' Index: 4 Value: 11
' Index: 5 Value: 8
' Index: 6 Value: 33554434
' Index: 7 Value: 52135
' Index: 8 Value: 9119016
' Index: 9 Value: 9109700
'**********************************************************************************************************
'In the output you will see that we have garbage data from element 5 to 9. Even though in this run
'the data in element 5 was preserved, it is still garbage, becuase it was only by chance the data
'remained the same. Whenever the array is expanded, the new elements should be zeroed out.
'Time to clean up. All we need to do at this point is to deallocate the pointer.
DeAllocate dyn1.arrptr
'And set the number of elements to zero.
dyn1.elcnt = 0
'As you can see, creating and maintaining a 1D dynamic array within a type def is quite easy, and you work
'with the pointer-based array just like you would any other array, the only difference being the subscript
'is at the end of the variable name and you use [] instead of ().