ZetCode

FreeBasic Callocate Keyword

last modified June 16, 2025

The FreeBasic Callocate keyword is used for dynamic memory allocation that automatically zero-initializes the allocated memory. It combines allocation and initialization in one operation.

Basic Definition

Callocate is a memory allocation function in FreeBasic that allocates a block of memory and initializes all bytes to zero. It is similar to Allocate but guarantees zero-initialization.

The function takes the number of elements and element size as parameters. It returns a pointer to the allocated memory block. If allocation fails, it returns a null pointer.

Allocating a Single Variable

This example shows basic usage of Callocate for a single variable.

callocate_single.bas
Dim p As Integer Ptr
p = Callocate(1, SizeOf(Integer))

If p = 0 Then
    Print "Memory allocation failed"
Else
    Print "Allocated value: "; *p  ' Will print 0
    *p = 42
    Print "Assigned value: "; *p
    Deallocate p
End If

Here we allocate memory for one integer. The allocated memory is initialized to zero. We check for allocation failure, then demonstrate the zero value before assigning our own value. Always deallocate when done.

Allocating an Array

Callocate is particularly useful for allocating arrays with zeroed elements.

callocate_array.bas
Dim arrSize As Integer = 5
Dim arr As Double Ptr = Callocate(arrSize, SizeOf(Double))

If arr = 0 Then
    Print "Array allocation failed"
Else
    For i As Integer = 0 To arrSize - 1
        Print "arr["; i; "] = "; arr[i]  ' All zeros
    Next
    Deallocate arr
End If

This allocates an array of 5 doubles, all initialized to 0.0. The example shows how to access array elements and verify their initial zero values. Remember to deallocate the memory when finished.

Allocating a Structure

Callocate ensures all structure members are zero-initialized.

callocate_struct.bas
Type Person
    name As String
    age As Integer
    height As Double
End Type

Dim p As Person Ptr = Callocate(1, SizeOf(Person))

If p = 0 Then
    Print "Structure allocation failed"
Else
    Print "Name: '"; p->name; "'"  ' Empty string
    Print "Age: "; p->age          ' 0
    Print "Height: "; p->height    ' 0.0
    Deallocate p
End If

This demonstrates allocating a structure with Callocate. All members are properly initialized - string to empty, integers to 0, and doubles to 0.0. This prevents uninitialized memory issues common with plain Allocate.

Allocating a 2D Array

Callocate can be used to create multi-dimensional arrays.

callocate_2darray.bas
Dim rows As Integer = 3
Dim cols As Integer = 4

Dim matrix As Integer Ptr Ptr = Callocate(rows, SizeOf(Integer Ptr))

For i As Integer = 0 To rows - 1
    matrix[i] = Callocate(cols, SizeOf(Integer))
Next

' Verify all elements are zero
For i As Integer = 0 To rows - 1
    For j As Integer = 0 To cols - 1
        Print matrix[i][j]; " ";
    Next
    Print
Next

' Cleanup
For i As Integer = 0 To rows - 1
    Deallocate matrix[i]
Next
Deallocate matrix

This creates a 3x4 integer matrix with all elements initialized to zero. First we allocate an array of pointers, then allocate each row. The nested loops verify initialization. Proper cleanup requires deallocating each row then the pointer array.

String Allocation

Callocate can be used to allocate buffers for string operations.

callocate_string.bas
Dim bufferSize As Integer = 256
Dim buffer As ZString Ptr = Callocate(bufferSize, SizeOf(ZString))

If buffer = 0 Then
    Print "Buffer allocation failed"
Else
    ' All bytes are zero, so string is empty
    Print "Buffer contents: '"; *buffer; "'"
    
    ' Copy a string into the buffer
    *buffer = "Hello, FreeBasic!"
    Print "New contents: '"; *buffer; "'"
    
    Deallocate buffer
End If

Here we allocate a zero-terminated string buffer. The initial contents are all zeros, making it an empty string. We demonstrate assigning a string to the buffer. ZString pointers are used for compatibility with C strings.

Checking Allocation Failure

Always check if Callocate returns a null pointer.

callocate_failure.bas
' Attempt unrealistically large allocation
Dim hugePtr As Byte Ptr = Callocate(1024*1024*1024, 1024*1024*1024)

If hugePtr = 0 Then
    Print "Allocation failed as expected"
Else
    Print "Unexpected successful allocation"
    Deallocate hugePtr
End If

This demonstrates proper error handling for failed allocations. We attempt an extremely large allocation that will fail. The code checks for and handles the null pointer return value. Never use memory without checking allocation.

Best Practices

This tutorial covered the FreeBasic Callocate keyword with practical examples showing its usage in different 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.