ZetCode

FreeBasic Preserve Keyword

last modified June 16, 2025

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's essential for maintaining data integrity when working with dynamic arrays.

Basic Array Resizing

This example shows the difference between resizing with and without Preserve.

preserve_basic.bas
Dim arr() As Integer = {1, 2, 3}

Redim arr(4)  ' Resize without Preserve
Print "Without Preserve:"
For i As Integer = LBound(arr) To UBound(arr)
    Print arr(i);
Next
Print

Dim arr2() As Integer = {1, 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.

preserve_strings.bas
Dim names() As String = {"Alice", "Bob", "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.

preserve_multidim.bas
Dim matrix(1 To 2, 1 To 2) As Integer
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.

preserve_udt.bas
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.

preserve_performance.bas
Dim bigArray(1000000) As Integer  ' 1 million elements

' 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
Dim newArray(2000000) As Integer
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's important to handle potential errors when using Preserve.

preserve_errors.bas
Dim arr() As Integer = {1, 2, 3}

' Attempt invalid resize (smaller array)
On Error Goto error_handler
Redim Preserve arr(1)
Print "Resize successful"
Exit Sub

error_handler:
Print "Error "; Err; ": "; Error(Err)
Resume Next

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

This tutorial covered the FreeBasic Preserve keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.