FreeBasic Allocate Keyword
Last modified October 18, 2026
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 is 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.
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.
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.
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.
Type Person
name As String
age As Integer
End Type
Dim p As Person Ptr
p = New Person
p->name = "John Doe"
p->age = 35
Print "Name: "; p->name
Print "Age: "; p->age
Delete p
We define a Person structure and create one instance with New.
The arrow operator (->) accesses structure members
through the pointer. New allocates the memory and runs the
constructor, so the String field is initialized correctly.
Delete runs the destructor and frees the memory. Using
Allocate directly with a UDT that contains a String
is unsafe, because Allocate does not initialize the memory and
the string descriptor contains random data.
Checking Allocation Success
Always verify that allocation succeeded before using the memory.
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.
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 not initialized by the resize itself; the example assigns them explicitly. Always use the returned pointer from
Reallocate.
Memory Leak Prevention
This example demonstrates proper memory management techniques.
Sub ProcessData()
Dim buf As Integer Ptr = Allocate(100 * SizeOf(Integer))
' Simulate processing
For i As Integer = 0 To 99
buf[i] = i
Next
' Oops! Forgot to deallocate
' Deallocate(buf) ' 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
- Check pointers: Always verify allocation succeeded.
- Match allocations: Every Allocate needs a Deallocate.
- Use SizeOf: Calculate sizes properly for portability.
- Initialize memory: Do not assume allocated memory is zeroed.
- Document ownership: Clearly indicate who frees memory.
This tutorial covered the FreeBasic Allocate keyword with
practical examples showing dynamic memory management techniques.
Author
List all FreeBasic Tutorials.