FreeBasic Any Keyword
Last modified October 18, 2026
The FreeBasic Any keyword has several uses: as Any Ptr
for generic pointer types that can point to any data type, as an initializer
(= Any) that leaves a variable or array uninitialized, and in array
bounds (Dim a(Any)) to declare dynamic arrays.
Basic Definition
In FreeBasic, Any Ptr is a pointer type that can point to any data type,
providing flexibility for functions that must accept different pointer types.
A pure Any Ptr is not type checked by the compiler; it must be cast to a
concrete pointer type (for example Integer Ptr) before it is dereferenced.
The = Any initializer is used with Dim to leave a variable or
array uninitialized, so it contains whatever was previously in memory.
The Any keyword is useful for creating generic functions that work with
pointers to different data types, and for declaring variables and 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.
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, and then use Cast to access the values through
the appropriate pointer type.
Any with Arrays
The Any keyword is used with arrays to indicate that they should not be
automatically initialized to zero.
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 is not guaranteed to contain zeros; it holds
whatever bytes were previously in that memory, which may happen to be zero. The regular
array is always initialized to zero.
Any Ptr in Function Parameters
Functions can accept Any Ptr parameters to handle pointers to different data types.
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.
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 show the two allocation styles: the = Any
array is left uninitialized, so its bytes are not guaranteed to be zero, while the
regular array is zeroed. Note that Sleep waits for a key press before the
program ends.
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.
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 scenarios where pointer flexibility and
uninitialized memory allocation are useful.
Author
List all FreeBasic Tutorials.