ZetCode

FreeBasic Any Keyword

last modified June 16, 2025

The FreeBasic Any keyword has two primary uses: as Any Ptr for generic pointer types that can accept pointers to any data type, and in array declarations to indicate uninitialized arrays.

Basic Definition

In FreeBasic, Any Ptr is a pointer type that can point to any data type, providing flexibility when working with functions that need to accept different pointer types. The Any keyword is also used in array declarations to specify that arrays should remain uninitialized.

The Any keyword is useful for creating generic functions that work with pointers to different data types, and for declaring arrays that should not be automatically initialized to zero.

Any Ptr Basic Usage

This example shows how to use Any Ptr to create a generic pointer that can point to different data types.

any_basic.bas
Dim As Integer value1 = 42
Dim As Double value2 = 3.14159
Dim As String value3 = "Hello"

Dim p As Any Ptr

p = @value1
Print "Integer value: "; *Cast(Integer Ptr, p)

p = @value2
Print "Double value: "; *Cast(Double Ptr, p)

p = @value3
Print "String value: "; *Cast(String Ptr, p)

Here we declare an Any Ptr variable that can point to different data types. We assign addresses of integer, double, and string variables to the same pointer, then use Cast to access the values through the appropriate pointer type.

Any with Arrays

The Any keyword is used with arrays to indicate they should not be automatically initialized to zero.

any_array.bas
Dim As Integer a(0 To 9) = Any  '' uninitialized array
Dim As Double d(0 To 4)         '' initialized to zero

Print "Uninitialized array values:"
For i As Integer = 0 To 9
    Print "a("; i; ") = "; a(i)
Next

Print "Initialized array values:"
For i As Integer = 0 To 4
    Print "d("; i; ") = "; d(i)
Next

This example shows the difference between initialized and uninitialized arrays. The array declared with = Any contains unpredictable values, while the regular array is automatically initialized to zero.

Any Ptr in Function Parameters

Functions can accept Any Ptr parameters to handle pointers to different data types.

any_function.bas
Declare Sub PrintBytes(ByVal x As Any Ptr)

Dim As Integer num = 1234
Dim As Double dbl = 3.14159
Dim As String text = "Hello"

PrintBytes(@num)
PrintBytes(@dbl)
PrintBytes(@text)

Sub PrintBytes(ByVal x As Any Ptr)
    Dim As Integer i
    Print "Raw bytes: ";
    For i = 0 To 7  '' Print first 8 bytes
        Print Cast(UByte Ptr, x)[i] & " ";
    Next
    Print
End Sub

The PrintBytes subroutine accepts an Any Ptr parameter, allowing it to work with pointers to different data types. It interprets the data as raw bytes, demonstrating how Any Ptr provides low-level access.

Complete Example with Any Ptr

This comprehensive example demonstrates both uses of the Any keyword: uninitialized arrays and generic pointer parameters.

any_complete.bas
Declare Sub echo(ByVal x As Any Ptr)

Dim As Integer a(0 To 9) = Any '' uninitialized array
Dim As Double  d(0 To 4)       '' initialized to zero

Dim p As Any Ptr

Dim pa As Integer Ptr = @a(0)
Print "Not initialized ";
echo pa       '' pass to echo a pointer to integer

Dim pd As Double Ptr = @d(0)
Print "Initialized ";
echo pd       '' pass to echo a pointer to double

p = pa     '' assign to p a pointer to integer
p = pd     '' assign to p a pointer to double

Sleep

Sub echo (ByVal x As Any Ptr)
    Dim As Integer i
    For i = 0 To 39
        'echo interprets the data in the pointer as bytes
        Print Cast(UByte Ptr, x)[i] & " ";
    Next
    Print
End Sub

This example shows the practical use of Any in FreeBasic. The echo subroutine accepts any pointer type and interprets the data as raw bytes. The arrays demonstrate the difference between initialized and uninitialized memory allocation.

Any Initialization Syntax

The Any keyword is also used to indicate that variables or arrays should not be initialized, leaving their contents unpredictable for performance reasons.

any_init.bas
Dim As Integer uninit = Any       '' not initialized
Dim As Integer init = 0           '' initialized to 0

Dim As Integer uninit_array(0 To 4) = Any  '' not initialized
Dim As Integer init_array(0 To 4)          '' initialized to 0

Print "Uninitialized variable: "; uninit
Print "Initialized variable: "; init

Print "Uninitialized array values:"
For i As Integer = 0 To 4
    Print "uninit_array["; i; "] = "; uninit_array(i)
Next

Print "Initialized array values:"
For i As Integer = 0 To 4
    Print "init_array["; i; "] = "; init_array(i)
Next

This example demonstrates using Any to prevent automatic initialization. Uninitialized variables and arrays contain whatever data was previously in memory, which can be useful for performance when you plan to immediately assign values.

This tutorial covered the FreeBasic Any keyword with practical examples showing its usage in different scenarios where pointer flexibility and uninitialized memory allocation are needed.

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.