ZetCode

FreeBasic Callocate 关键字

最后修改日期:2025 年 6 月 16 日

FreeBasic 的 Callocate 关键字用于动态内存分配,该分配会自动将分配的内存初始化为零。它在一个操作中结合了分配和初始化。

基本定义

Callocate 是 FreeBasic 中的一个内存分配函数,它分配一块内存并将所有字节初始化为零。它类似于 Allocate,但保证了零初始化。

该函数以元素数量和元素大小作为参数。它返回指向已分配内存块的指针。如果分配失败,则返回空指针。

分配单个变量

此示例展示了 Callocate 用于单个变量的基本用法。

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

在这里,我们为一个整数分配内存。分配的内存被初始化为零。我们检查分配是否失败,然后演示零值,再为其分配自己的值。完成后务必释放内存。

分配数组

Callocate 对于分配带有零初始化的元素的数组特别有用。

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

这分配了一个包含 5 个双精度数的数组,所有元素都初始化为 0.0。该示例展示了如何访问数组元素并验证它们的初始零值。请记住,完成后要释放内存。

分配结构

Callocate 确保所有结构成员都已初始化为零。

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

这演示了使用 Callocate 分配结构。所有成员都已正确初始化——字符串为空,整数为 0,双精度数为 0.0。这可以避免普通 Allocate 常见的未初始化内存问题。

分配二维数组

Callocate 可用于创建多维数组。

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

这创建了一个 3x4 的整数矩阵,所有元素都初始化为零。我们首先分配一个指针数组,然后分配每一行。嵌套循环验证了初始化。正确的清理需要先释放每一行,然后释放指针数组。

字符串分配

Callocate 可用于为字符串操作分配缓冲区。

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

在这里,我们分配了一个以零结尾的字符串缓冲区。初始内容全为零,使其成为一个空字符串。我们演示了如何将字符串赋值给缓冲区。ZString 指针用于与 C 字符串兼容。

检查分配失败

始终检查 Callocate 是否返回空指针。

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

这演示了对失败分配的正确错误处理。我们尝试进行一次过大的分配,该分配将会失败。代码检查并处理空指针返回值。切勿在未检查分配的情况下使用内存。

最佳实践

本教程通过实际示例介绍了 FreeBasic 的 Callocate 关键字,展示了它在不同场景下的用法。

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。至今,我已撰写了 1,400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出所有 FreeBasic 教程