FreeBasic Reallocate Keyword
last modified June 16, 2025
The FreeBasic Reallocate keyword is used to resize dynamically
allocated memory blocks. It allows adjusting memory allocation while
preserving existing data when possible.
Basic Definition
Reallocate changes the size of a previously allocated memory
block. It takes a pointer to existing memory and a new size as parameters.
The function returns a pointer to the resized memory block.
If the new size is larger, the additional memory is uninitialized. If smaller, the excess memory is freed. The original data is preserved up to the smaller of the old and new sizes.
Basic Reallocate Example
This example demonstrates the simplest usage of Reallocate.
Dim p As Integer Ptr = Allocate(10 * SizeOf(Integer))
For i As Integer = 0 To 9
p[i] = i * 10
Next
' Resize the array to hold 15 elements
p = Reallocate(p, 15 * SizeOf(Integer))
' Initialize new elements
For i As Integer = 10 To 14
p[i] = i * 10
Next
' Print all elements
For i As Integer = 0 To 14
Print p[i]
Next
Deallocate(p)
Here we first allocate memory for 10 integers and fill them with values. Then we resize the memory block to hold 15 integers. The original values are preserved, and we initialize the new elements.
Reallocate with Strings
Reallocate can be used with string buffers for dynamic string
operations.
Dim buffer As ZString Ptr = Allocate(20) *buffer = "Hello, there!" Print "Original: "; *buffer Print "Length: "; Len(*buffer) ' Resize buffer to hold longer string buffer = Reallocate(buffer, 50) *buffer = "Hello, FreeBasic Programming Language!" Print "Resized: "; *buffer Print "New length: "; Len(*buffer) Deallocate(buffer)
This example shows how to resize a string buffer. We first allocate 20 bytes for a short string, then resize to 50 bytes for a longer string. The original content is preserved until we overwrite it.
Reallocate with Structures
Reallocate works with custom structure types as well.
Type Person
name As String
age As Integer
End Type
Dim people As Person Ptr = Allocate(3 * SizeOf(Person))
' Initialize first three people
people[0].name = "Alice": people[0].age = 25
people[1].name = "Bob": people[1].age = 30
people[2].name = "Charlie": people[2].age = 35
' Resize to hold five people
people = Reallocate(people, 5 * SizeOf(Person))
' Initialize new people
people[3].name = "David": people[3].age = 40
people[4].name = "Eve": people[4].age = 45
' Print all
For i As Integer = 0 To 4
Print people[i].name; " is "; people[i].age; " years old"
Next
Deallocate(people)
This code demonstrates resizing an array of structures. We start with memory for 3 Person structures, then resize to hold 5. The original data remains intact, and we can add new elements to the expanded array.
Reallocate with Error Checking
Always check if Reallocate succeeded, as it can fail.
Dim p As Integer Ptr = Allocate(100 * SizeOf(Integer))
If p = 0 Then
Print "Initial allocation failed"
End
End If
' Try to resize to very large block
Dim newPtr As Integer Ptr = Reallocate(p, 1000000000 * SizeOf(Integer))
If newPtr = 0 Then
Print "Reallocation failed - keeping original block"
' Continue with original pointer
Else
p = newPtr
Print "Reallocation succeeded"
End If
Deallocate(p)
This example shows proper error handling. We check both the initial allocation and reallocation results. If reallocation fails, we can continue using the original memory block or handle the error appropriately.
Shrinking Arrays with Reallocate
Reallocate can reduce memory usage by shrinking arrays.
Dim arr As Double Ptr = Allocate(1000 * SizeOf(Double))
' Fill array with values
For i As Integer = 0 To 999
arr[i] = Rnd() * 100
Next
' We only need first 100 elements now
arr = Reallocate(arr, 100 * SizeOf(Double))
' Verify the first elements are preserved
For i As Integer = 0 To 99
Print arr[i]
Next
Deallocate(arr)
Here we allocate a large array, then shrink it to a smaller size when we don't need all the space anymore. The first elements remain unchanged, while the excess memory is freed.
Reallocate with Multi-dimensional Arrays
For multi-dimensional arrays, reallocation requires careful pointer handling.
Dim rows As Integer = 3, cols As Integer = 4
Dim matrix As Integer Ptr Ptr = Allocate(rows * SizeOf(Integer Ptr))
' Allocate each row
For i As Integer = 0 To rows - 1
matrix[i] = Allocate(cols * SizeOf(Integer))
For j As Integer = 0 To cols - 1
matrix[i][j] = i * cols + j
Next
Next
' Resize to more rows
rows = 5
matrix = Reallocate(matrix, rows * SizeOf(Integer Ptr))
' Allocate new rows
For i As Integer = 3 To rows - 1
matrix[i] = Allocate(cols * SizeOf(Integer))
For j As Integer = 0 To cols - 1
matrix[i][j] = i * cols + j
Next
Next
' Print all elements
For i As Integer = 0 To rows - 1
For j As Integer = 0 To cols - 1
Print Using "####"; matrix[i][j];
Next
Print
Next
' Cleanup
For i As Integer = 0 To rows - 1
Deallocate(matrix[i])
Next
Deallocate(matrix)
This complex example shows how to resize a 2D array. We first allocate a 3x4 matrix, then resize to 5 rows. The original rows are preserved, and we allocate and initialize new rows. Each row must be managed separately.
Reallocate vs Allocate/Free
Reallocate is more efficient than manual allocate/copy/free
for resizing memory blocks. It often avoids full memory copies when possible.
Dim size As Integer = 1000000
Dim p1 As Integer Ptr = Allocate(size * SizeOf(Integer))
Dim p2 As Integer Ptr
' Fill with data
For i As Integer = 0 To size - 1
p1[i] = i
Next
' Method 1: Using Reallocate
Dim t As Double = Timer
p1 = Reallocate(p1, 2 * size * SizeOf(Integer))
Print "Reallocate time: "; Timer - t
' Method 2: Manual allocate/copy/free
t = Timer
p2 = Allocate(2 * size * SizeOf(Integer))
For i As Integer = 0 To size - 1
p2[i] = p1[i]
Next
Deallocate(p1)
Print "Manual time: "; Timer - t
Deallocate(p2)
This benchmark compares Reallocate with manual memory management.
Reallocate is typically faster as it can often extend memory in place without
copying. The performance difference grows with larger memory blocks.
Best Practices
- Check for NULL: Always verify Reallocate didn't return NULL.
- Preserve pointer: Use a temporary variable to store the result.
- Size calculation: Always use SizeOf for type safety.
- Initialization: Remember new memory is uninitialized.
- Cleanup: Don't forget to Deallocate when done.
This tutorial covered the FreeBasic Reallocate keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.