Category: Runtime Helpers
Resize(arrayReference, size As Integer)
Nothing
Changes the length of a one-dimensional array.
The size argument is the new number of elements in the array. For example, Resize(values, 10) makes room for 10 elements, so the valid indexes become 0 through 9.
Resize keeps the old values that still fit. If you make the array larger, the new slots start empty. If you make the array smaller, any elements beyond the new length are removed.
The first argument must be a one-dimensional array reference. That can be an array variable or an array field on a custom type object, including nested member paths such as Resize(world.Player.InventorySlots, 16). Multi-dimensional arrays are not currently supported by Resize.
If your array stores numbers or booleans, shrinking the array is simple: the removed values are just thrown away.
If your array stores custom types, each array slot holds an object reference. When you shrink the array, the removed slots disappear. If one of those removed slots was the only place that still pointed to an object, your program no longer has a way to call Free on that object.
That situation can cause a memory leak. A memory leak means memory stays allocated even though your program can no longer use it.
Beginner-friendly rule: if you are about to shrink an array of custom types, Free any objects in the part you are going to remove before you call Resize.
Dim values(3) As Integer
values[0] = 10
values[1] = 20
Resize(values, 6)
After this call, values[0] is still 10, values[1] is still 20, and the array now has 6 elements.
Type Player
Field Score As Integer
End Type
Dim players(5) As Player
players[3] = New Player
players[4] = New Player
Free(players[4])
Resize(players, 4)
Here, the object in players[4] is freed before the array is shrunk, so that reference is not lost.