ZetCode

FreeBasic Allocate Keyword

last modified June 16, 2025

The FreeBasic Allocate keyword is used for dynamic memory allocation during program execution. It allows programs to request memory from the heap at runtime. This is essential for flexible data structures.

Basic Definition

Allocate reserves a block of memory of specified size and returns a pointer to it. The allocated memory remains reserved until explicitly freed with Deallocate. This enables dynamic data structures that grow as needed.

Dynamic allocation is crucial when the required memory size isn't known at compile time. It's used for arrays, strings, and complex data structures that need to resize during execution.

Basic Memory Allocation

This example shows the simplest use of Allocate for a single integer.

allocate_basic.bas
Dim p As Integer Ptr
p = Allocate(SizeOf(Integer))

*p = 42
Print "Value at allocated memory: "; *p

Deallocate(p)

We declare an integer pointer and allocate memory for one integer. The SizeOf operator ensures we request the correct amount. We store a value, print it, and then free the memory. Always deallocate to prevent memory leaks.

Allocating Arrays

Allocate can create dynamic arrays whose size is determined at runtime.

allocate_array.bas
Dim size As Integer = 5
Dim arr As Integer Ptr
arr = Allocate(size * SizeOf(Integer))

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

Deallocate(arr)

Here we allocate an array of 5 integers. The size is calculated by multiplying the element count by element size. We access elements using array notation. The memory is properly freed at the end.

Allocating Strings

Strings can be dynamically allocated to handle variable-length text.

allocate_string.bas
Dim text As ZString Ptr
Dim length As Integer = 20

text = Allocate(length + 1)  ' +1 for null terminator
*text = "FreeBasic Tutorial"

Print "Allocated string: "; *text
Deallocate(text)

This allocates memory for a null-terminated string. We add 1 to the length for the null terminator. The string is assigned and printed. ZString pointers are used for compatibility with C-style strings.

Allocating Structures

Complex data types like structures can be dynamically allocated.

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

Dim p As Person Ptr
p = Allocate(SizeOf(Person))

p->name = "John Doe"
p->age = 35

Print "Name: "; p->name
Print "Age: "; p->age

Deallocate(p)

We define a Person structure and allocate memory for one instance. The arrow operator (->) accesses structure members through the pointer. The memory is properly sized using SizeOf.

Checking Allocation Success

Always verify that allocation succeeded before using the memory.

allocate_check.bas
Dim hugeArray As Integer Ptr
hugeArray = Allocate(1000000000 * SizeOf(Integer))

If hugeArray = 0 Then
    Print "Memory allocation failed!"
Else
    Print "Memory allocated successfully"
    Deallocate(hugeArray)
End If

This attempts to allocate a very large array. If allocation fails, the pointer will be null (0). We check this condition before using the memory. This prevents crashes from null pointer dereferencing.

Resizing Allocated Memory

FreeBasic allows resizing allocated memory blocks with Reallocate.

allocate_resize.bas
Dim arr As Integer Ptr
Dim size As Integer = 3

arr = Allocate(size * SizeOf(Integer))
For i As Integer = 0 To size - 1
    arr[i] = i + 1
Next

' Need more space
size = 6
arr = Reallocate(arr, size * SizeOf(Integer))

For i As Integer = 3 To size - 1
    arr[i] = i + 1
Next

For i As Integer = 0 To size - 1
    Print arr[i];
Next
Print

Deallocate(arr)

We first allocate memory for 3 integers, then resize to hold 6. The original contents are preserved during reallocation. New elements are initialized after resizing. Always use the returned pointer from Reallocate.

Memory Leak Prevention

This example demonstrates proper memory management techniques.

allocate_leak.bas
Sub ProcessData()
    Dim data As Integer Ptr = Allocate(100 * SizeOf(Integer))
    
    ' Simulate processing
    For i As Integer = 0 To 99
        data[i] = i
    Next
    
    ' Oops! Forgot to deallocate
    ' Deallocate(data) ' Uncomment to fix
End Sub

ProcessData()
Print "Memory leaked if not deallocated"

This shows a common memory leak scenario. The allocated memory isn't freed before the subroutine ends. Uncomment the Deallocate line to fix. Always pair allocations with deallocations.

Best Practices

This tutorial covered the FreeBasic Allocate keyword with practical examples showing dynamic 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.