FreeBasic Cptr Keyword
last modified June 16, 2025
The FreeBasic Cptr
keyword is used for pointer type conversion.
It allows casting between different pointer types while maintaining the same
memory address. This is useful for low-level memory operations.
Basic Definition
Cptr
stands for "cast pointer" and provides a way to convert
between pointer types without changing the actual memory address. It's similar
to C-style pointer casting but with FreeBasic syntax.
The general syntax is Cptr(target_type, expression)
. The expression
must evaluate to a pointer value. The target type must be a pointer type.
Basic Pointer Conversion
This example shows the simplest use of Cptr to convert between pointer types.
Dim i As Integer = 42 Dim pi As Integer Ptr = @i Dim pv As Any Ptr = Cptr(Any Ptr, pi) Print "Original value: "; *pi Print "Converted pointer: "; pv Print "Dereferenced: "; *Cptr(Integer Ptr, pv)
Here we create an integer and get its pointer. We then use Cptr to convert the integer pointer to a generic pointer (Any Ptr). Finally, we convert it back to access the original value. The memory address remains unchanged.
Accessing Memory as Different Types
Cptr can reinterpret memory as different types, similar to unions in C.
Dim value As Integer = &H41424344 Dim p As Byte Ptr = Cptr(Byte Ptr, @value) Print "Integer value: "; Hex(value) Print "Bytes: "; For i As Integer = 0 To 3 Print Hex(p[i]); " "; Next
This code treats an integer as an array of bytes. We use Cptr to get a byte pointer to the integer. This lets us examine each byte individually, showing how the value is stored in memory.
Working with Structures
Cptr is useful when working with structures and needing to access members through different pointer types.
Type Point x As Integer y As Integer End Type Dim pt As Point = (10, 20) Dim p As Any Ptr = @pt Dim px As Integer Ptr = Cptr(Integer Ptr, p) Print "x: "; px[0] Print "y: "; px[1]
We create a Point structure and get a generic pointer to it. Using Cptr, we convert this to an integer pointer to access the x and y members as array elements. This demonstrates direct memory access to structure members.
Function Pointer Conversion
Cptr can be used with function pointers for calling functions through different pointer types.
Function AddNumbers(a As Integer, b As Integer) As Integer Return a + b End Function Dim func As Function(a As Integer, b As Integer) As Integer = @AddNumbers Dim p As Any Ptr = Cptr(Any Ptr, func) Dim result As Integer = Cptr(Function(a As Integer, b As Integer) As Integer, p)(3, 4) Print "3 + 4 = "; result
This example shows converting between function pointer types. We store the function address in a generic pointer, then convert it back to call the function. This technique is useful for callback mechanisms.
Working with Arrays
Cptr can help when you need to treat an array as a different type.
Dim bytes(3) As Byte = {65, 66, 67, 0} Dim p As Integer Ptr = Cptr(Integer Ptr, @bytes(0)) Print "As string: "; bytes Print "As integer: "; Hex(*p)
Here we have a byte array containing ASCII characters. Using Cptr, we treat the array as an integer pointer. This lets us see how the bytes are interpreted when read as an integer value.
Interfacing with C Libraries
Cptr is particularly useful when calling C functions that expect specific pointer types.
Extern "C" Declare Sub c_function Lib "mylib" (p As Byte Ptr) End Extern Dim buffer As String = "Hello" c_function(Cptr(Byte Ptr, Strptr(buffer)))
This demonstrates using Cptr to pass a FreeBasic string pointer to a C function expecting a byte pointer. The Strptr gets the string's character data, and Cptr ensures the correct pointer type is passed.
Pointer Arithmetic
Cptr can be used with pointer arithmetic to navigate memory structures.
Dim values(3) As Integer = {10, 20, 30, 40} Dim p As Any Ptr = @values(0) For i As Integer = 0 To 3 Print "Value "; i; ": "; *Cptr(Integer Ptr, p + i * SizeOf(Integer)) Next
This example shows how to use Cptr with pointer arithmetic. We convert the generic pointer to an integer pointer at each offset to access array elements. The SizeOf operator ensures correct byte offsets.
Best Practices
- Type Safety: Use Cptr only when necessary and be aware of type sizes.
- Alignment: Ensure memory is properly aligned for the target type.
- Documentation: Comment Cptr usage to explain why conversion is needed.
- Alternatives: Consider using unions or proper type definitions first.
- Debugging: Be cautious as improper Cptr usage can cause hard-to-find bugs.
This tutorial covered the FreeBasic Cptr
keyword with practical
examples showing its usage in different scenarios requiring pointer conversion.
Author
List all FreeBasic Tutorials.