FreeBasic Preserve Keyword
Last modified April 20, 2026
The FreeBasic Preserve keyword is used with Redim
to resize dynamic arrays while keeping their existing contents. Without
Preserve, resizing an array erases all its elements.
Basic Definition
In FreeBasic, Preserve is a modifier for the Redim
statement. It ensures that existing array elements are preserved when
changing the array's dimensions.
The Preserve keyword can only be used when changing the last
dimension of a multi-dimensional array. It is essential for maintaining
data integrity when working with dynamic arrays.
Basic Array Resizing
This example shows the difference between resizing with and without Preserve.
Dim arr() As Integer Redim arr(2) arr(0) = 1 arr(1) = 2 arr(2) = 3 Print "Before Redim (no Preserve):" For i As Integer = LBound(arr) To UBound(arr) Print arr(i); Next Print Redim arr(4) ' Resize without Preserve (reinitializes contents) Print "After Redim (no Preserve):" For i As Integer = LBound(arr) To UBound(arr) Print arr(i); Next Print Dim arr2() As Integer Redim arr2(2) arr2(0) = 1 arr2(1) = 2 arr2(2) = 3 Redim Preserve arr2(4) ' Resize with Preserve Print "With Preserve:" For i As Integer = LBound(arr2) To UBound(arr2) Print arr2(i); Next
The first array loses its values when resized without Preserve. The second array keeps its original values in the resized array. New elements are initialized to zero for numeric types.
Preserving String Arrays
The Preserve keyword works similarly with string arrays.
Dim names() As String
Redim names(2)
names(0) = "Alice"
names(1) = "Bob"
names(2) = "Charlie"
Redim Preserve names(5)
names(3) = "David"
names(4) = "Eve"
For i As Integer = LBound(names) To UBound(names)
Print i; ": "; names(i)
Next
This example shows how string arrays preserve their contents when resized. Original elements remain intact while new elements are empty strings by default. We then fill some of the new positions.
Multi-dimensional Arrays
With multi-dimensional arrays, only the last dimension can be resized.
Dim matrix() As Integer
Redim matrix(1 To 2, 1 To 2)
matrix(1, 1) = 1
matrix(1, 2) = 2
matrix(2, 1) = 3
matrix(2, 2) = 4
Redim Preserve matrix(1 To 2, 1 To 3) ' Only last dimension can change
' Fill new elements
matrix(1, 3) = 5
matrix(2, 3) = 6
For i As Integer = 1 To 2
For j As Integer = 1 To 3
Print matrix(i, j); " ";
Next
Print
Next
This demonstrates resizing a 2D array while preserving contents. Note that we can only change the last dimension (columns in this case). Attempting to change the first dimension would cause an error.
Preserve with User-Defined Types
Arrays of user-defined types can also be preserved during resizing.
Type Person
name As String
age As Integer
End Type
Dim people() As Person
Redim people(2)
people(0).name = "Alice" : people(0).age = 25
people(1).name = "Bob" : people(1).age = 30
Redim Preserve people(4)
people(2).name = "Charlie" : people(2).age = 35
For i As Integer = LBound(people) To UBound(people)
If Len(people(i).name) > 0 Then
Print people(i).name; " ("; people(i).age; ")"
Else
Print "[Empty]"
End If
Next
This example shows preserving an array of custom types. Existing Person records remain intact after resizing. New elements are initialized with default values (empty string for name, 0 for age).
Performance Considerations
Using Preserve has some performance implications worth noting.
' Declare as variable-length (dynamic) arrays
Dim bigArray() As Integer
Dim newArray() As Integer
' Allocate initial sizes (moves them to the heap, avoiding stack warnings)
ReDim bigArray(1000000)
ReDim newArray(2000000)
' Fill array with values
For i As Integer = LBound(bigArray) To UBound(bigArray)
bigArray(i) = i
Next
' Time resizing with Preserve
Dim t As Double = Timer
ReDim Preserve bigArray(2000000)
Print "Time to resize with Preserve: "; Timer - t; " seconds"
' Compare to creating new array
Dim t2 As Double = Timer
' newArray is already allocated above, but if you wanted a fresh alloc:
' ReDim newArray(2000000) ' (uncomment if benchmarking allocation time)
For i As Integer = 0 To 1000000
newArray(i) = i
Next
Print "Time to create new array: "; Timer - t2; " seconds"
This benchmark compares resizing with Preserve versus creating a new array. Preserve can be slower for large arrays because it must copy all existing elements. For frequent resizing, consider alternative data structures.
Error Handling with Preserve
It is important to handle potential errors when using Preserve.
Dim arr() As Integer Redim arr(2) arr(0) = 1 arr(1) = 2 arr(2) = 3 ' Attempt invalid resize (smaller array) On Error Goto error_handler Redim Preserve arr(1) Print "Resize successful" Goto finished error_handler: Print "Error code: "; Err Goto finished finished:
This example shows error handling when resizing fails. Trying to make an array smaller with Preserve causes an error. Always check array bounds before resizing and implement proper error handling.
Best Practices
- Minimize resizes: Avoid frequent small resizes; estimate needed size upfront.
- Check bounds: Always verify new dimensions are valid.
- Error handling: Implement error handling for resize operations.
- Multi-dimensional: Remember only last dimension can be changed.
- Alternatives: Consider linked lists for frequently changing data.
This tutorial covered the FreeBasic Preserve keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.