ZetCode

FreeBasic Erase Keyword

last modified June 16, 2025

The FreeBasic Erase keyword is used to clear or reset arrays. It can empty dynamic arrays or reset fixed-size arrays to their default values. This is useful for reusing arrays without redeclaring them.

Basic Definition

In FreeBasic, Erase is a statement that operates on arrays. For dynamic arrays, it deallocates memory and sets the array to zero elements. For fixed arrays, it resets all elements to their default values.

The Erase statement helps manage memory efficiently and provides a clean way to reset array contents. It's particularly useful when working with large arrays that need periodic clearing.

Erasing a Dynamic Array

This example shows how to erase a dynamic array, freeing its memory.

erase_dynamic.bas
Dim numbers() As Integer
ReDim numbers(5)

For i As Integer = 0 To 5
    numbers(i) = i * 10
Next

Print "Before erase:"
Print "Array size: "; UBound(numbers) - LBound(numbers) + 1

Erase numbers

Print "After erase:"
Print "Array size: "; UBound(numbers) - LBound(numbers) + 1

Here we create a dynamic array, fill it with values, then erase it. After erasing, the array has 0 elements. Attempting to access elements after erasing would cause an error since the array no longer exists.

Erasing a Fixed-Size Array

This example demonstrates erasing a fixed-size array.

erase_fixed.bas
Dim colors(4) As String
colors(0) = "red"
colors(1) = "green"
colors(2) = "blue"

Print "Before erase:"
For i As Integer = 0 To 4
    Print i; ": "; colors(i)
Next

Erase colors

Print "After erase:"
For i As Integer = 0 To 4
    Print i; ": "; colors(i)
Next

The fixed array maintains its size after erasing, but all elements are reset to empty strings (for string arrays) or zero (for numeric arrays). This is different from dynamic arrays which are completely deallocated.

Erasing a Multi-Dimensional Array

The Erase statement works with multi-dimensional arrays too.

erase_multidimensional.bas
Dim matrix(2, 2) As Integer

' Fill matrix
For i As Integer = 0 To 2
    For j As Integer = 0 To 2
        matrix(i, j) = i * 3 + j + 1
    Next
Next

Print "Before erase:"
For i As Integer = 0 To 2
    For j As Integer = 0 To 2
        Print matrix(i, j); " ";
    Next
    Print
Next

Erase matrix

Print "After erase:"
For i As Integer = 0 To 2
    For j As Integer = 0 To 2
        Print matrix(i, j); " ";
    Next
    Print
Next

This example creates a 3x3 matrix, fills it with values, then erases it. After erasing, all elements are reset to 0. The array dimensions remain unchanged, only the contents are cleared.

Erasing an Array of User-Defined Types

Erase also works with arrays of user-defined types.

erase_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim people(2) As Person

people(0).name = "Alice"
people(0).age = 25
people(1).name = "Bob"
people(1).age = 30

Print "Before erase:"
For i As Integer = 0 To 2
    Print people(i).name; " "; people(i).age
Next

Erase people

Print "After erase:"
For i As Integer = 0 To 2
    Print people(i).name; " "; people(i).age
Next

After erasing, all fields of the user-defined type are reset to their default values. Strings become empty, numbers become 0, and any nested types are similarly reset.

Erasing Multiple Arrays

You can erase multiple arrays with a single Erase statement.

erase_multiple.bas
Dim a(5) As Integer
Dim b(3) As String
Dim c() As Double
ReDim c(4)

' Fill arrays
For i As Integer = 0 To 5
    a(i) = i
Next

For i As Integer = 0 To 3
    b(i) = "Item " & i
Next

For i As Integer = 0 To 4
    c(i) = i * 1.5
Next

Erase a, b, c

Print "Array sizes after erase:"
Print "a: "; UBound(a) - LBound(a) + 1
Print "b: "; UBound(b) - LBound(b) + 1
Print "c: "; UBound(c) - LBound(c) + 1

This example shows how to erase three different arrays with one statement. Fixed arrays maintain their size but are reset, while the dynamic array is completely deallocated. The comma-separated syntax is efficient for clearing multiple arrays at once.

Erasing and Reusing Dynamic Arrays

This demonstrates the full lifecycle of a dynamic array with erase.

erase_reuse.bas
Dim values() As Integer

' First allocation
ReDim values(10)
For i As Integer = 0 To 10
    values(i) = i * 2
Next
Print "First size: "; UBound(values)

' Erase and reuse
Erase values
ReDim values(5)
For i As Integer = 0 To 5
    values(i) = i * 3
Next
Print "Second size: "; UBound(values)

' Final erase
Erase values
Print "Final size: ";
On Error Goto error_handler
Print UBound(values)
Exit Sub

error_handler:
Print "Error: Array not allocated"

This shows how to erase a dynamic array and then reuse it with different dimensions. After the final erase, attempting to access the array causes an error, demonstrating that it's truly deallocated. Error handling is needed for safe array access checks.

Best Practices

This tutorial covered the FreeBasic Erase keyword with practical examples showing its usage with different array types and 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.