ZetCode

FreeBasic Deallocate Keyword

Last modified October 18, 2026

The FreeBasic Deallocate keyword is used to free dynamically allocated memory. It works with pointers created using Allocate or Callocate. Proper memory management prevents memory leaks.

Basic Definition

Deallocate releases memory previously allocated with Allocate (it is the counterpart of Allocate; Callocate memory is released with it too). It returns the memory to the system. Failing to deallocate memory leads to memory leaks in programs.

The procedure takes a pointer as its argument. After deallocation, the pointer becomes invalid and should not be used; dereferencing it or calling Deallocate on it again is undefined behavior. Calling Deallocate on a null pointer does nothing. Always set pointers to zero after deallocation for safety.

Basic Deallocation

This example shows the simplest usage of the Deallocate keyword.

deallocate_basic.bas
Dim p As Integer Ptr
p = Allocate(SizeOf(Integer))
*p = 42

Print "Value before deallocation: "; *p

Deallocate(p)
p = 0 ' Good practice to null the pointer

' Accessing the freed memory through the old pointer would be undefined behavior
Print "Memory has been freed"

Here we allocate memory for an integer, store a value, then deallocate it. The pointer is set to null afterward. Attempting to use the pointer after deallocation would cause undefined behavior.

Deallocating Arrays

Dynamic arrays must be properly deallocated to prevent memory leaks.

deallocate_array.bas
Dim arr As Integer Ptr
arr = Allocate(10 * SizeOf(Integer))

For i As Integer = 0 To 9
    arr[i] = i * 10
    Print arr[i]; " ";
Next
Print

Deallocate(arr)
arr = 0

This code allocates memory for 10 integers, fills them with values, then frees the memory. Array-style access works with pointers in FreeBasic. Always deallocate arrays when they're no longer needed.

Deallocate in Functions

Memory allocated in functions should be properly managed to avoid leaks.

deallocate_function.bas
Function CreateBuffer(size As Integer) As Byte Ptr
    Return Allocate(size)
End Function

Dim buffer As Byte Ptr = CreateBuffer(100)
Print "Buffer created with size 100"

' Use the buffer here...

Deallocate(buffer)
buffer = 0
Print "Buffer freed"

This demonstrates memory management across functions. The function allocates memory which the caller must eventually deallocate. Document clearly who owns memory and is responsible for freeing it.

Checking Before Deallocation

It is good practice to check pointers before deallocation.

deallocate_check.bas
Dim buf As Byte Ptr = 0
Dim needed As Integer = 1

' Only allocate if needed
If needed Then
    buf = Allocate(100)
End If

' Safely deallocate
If buf Then
    Deallocate(buf)
    buf = 0
End If

This example shows defensive programming with pointers. We check if the pointer is non-null before deallocation. Calling Deallocate on a null pointer is a no-op in FreeBASIC, but the check still documents intent. The check does not protect against double-free: a second Deallocate on the same non-null address is undefined behavior, so setting the pointer to zero right after freeing is what prevents it.

Deallocating Structures

Complex structures with nested allocations require careful deallocation.

deallocate_structure.bas
Type Person
    name As String Ptr
    age As Integer Ptr
End Type

Dim p As Person Ptr = Allocate(SizeOf(Person))
p->name = Allocate(50)
p->age = Allocate(SizeOf(Integer))

' Use the structure...

Deallocate(p->name)
Deallocate(p->age)
Deallocate(p)
p = 0

When deallocating structures with pointer members, free the members first. Then free the structure itself. This prevents memory leaks in complex data structures with multiple allocations. Note that the members here are raw buffers; a String Ptr that points to a live string must have the string destroyed (set to "") before its descriptor memory is deallocated.

Deallocate vs Erase

Deallocate differs from Erase for dynamic arrays.

deallocate_vs_erase.bas
' Dynamic array using Redim
Dim array() As Integer
Redim array(0 To 9)

' Pointer-based array
Dim parray As Integer Ptr = Allocate(10 * SizeOf(Integer))

' Free them differently
Erase array ' For dynamic arrays
Deallocate(parray) ' For pointer arrays
parray = 0

This shows the difference between deallocating pointer arrays and erasing dynamic arrays. Erase is for dynamic arrays created with Dim and Redim, while Deallocate is for pointer-based allocations.

Memory Leaks

Proper deallocation helps prevent memory leaks in long-running programs. FreeBASIC does not report leaks by itself; the example below only shows the pattern.

deallocate_leak.bas
Sub ProcessData()
    Dim buf As Byte Ptr = Allocate(1024)
    ' ... use buf ...
    Deallocate(buf) ' Comment this to create a leak
    buf = 0
End Sub

For i As Integer = 1 To 100
    ProcessData()
Next

Print "Memory operations complete"

This demonstrates how forgetting to deallocate causes leaks. Each call allocates memory; without deallocation, memory usage grows. Always free memory in the same scope where it is allocated when possible.

Best Practices

This tutorial covered the FreeBasic Deallocate keyword with practical examples showing proper memory management techniques.

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.