ZetCode

FreeBasic New Keyword

last modified June 16, 2025

The FreeBasic New keyword is used for dynamic memory allocation. It creates objects on the heap and returns a pointer to the allocated memory.

Basic Definition

In FreeBasic, New allocates memory for an object or array at runtime. The allocated memory persists until explicitly freed with Delete.

Dynamic allocation is useful when the required memory size isn't known at compile time or when objects need to outlive their current scope. Proper memory management is crucial to avoid leaks.

Allocating a Single Object

This example shows basic usage of New to allocate a single integer.

new_single.bas
Dim p As Integer Ptr = New Integer
*p = 42

Print "Value: "; *p

Delete p

Here we allocate memory for one integer using New. We store the returned pointer, assign a value through it, print the value, and finally free the memory with Delete. Always pair New with Delete.

Allocating an Array

New can allocate arrays by specifying the size in square brackets.

new_array.bas
Dim size As Integer = 5
Dim arr As Integer Ptr = New Integer[size]

For i As Integer = 0 To size - 1
    arr[i] = i * 10
Next

For i As Integer = 0 To size - 1
    Print "arr["; i; "] = "; arr[i]
Next

Delete[] arr

This allocates an array of 5 integers. Note we use Delete[] for array deallocation. Array elements are accessed using pointer arithmetic or array notation. The memory is contiguous like static arrays.

Allocating User Types

New works with user-defined types (UDTs) just like built-in types.

new_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim p As Person Ptr = New Person
p->name = "Alice"
p->age = 30

Print "Name: "; p->name
Print "Age: "; p->age

Delete p

We allocate a Person object and access its members using the -> operator. The syntax is similar to C/C++. UDT allocation is essential for object-oriented programming in FreeBasic.

Constructor Initialization

New can call constructors when allocating objects with constructors.

new_constructor.bas
Type Vector
    x As Single
    y As Single
    
    Declare Constructor()
    Declare Constructor(x As Single, y As Single)
End Type

Constructor Vector()
    this.x = 0
    this.y = 0
End Constructor

Constructor Vector(x As Single, y As Single)
    this.x = x
    this.y = y
End Constructor

Dim v1 As Vector Ptr = New Vector
Dim v2 As Vector Ptr = New Vector(3.5, 4.2)

Print "v1: ("; v1->x; ", "; v1->y; ")"
Print "v2: ("; v2->x; ", "; v2->y; ")"

Delete v1
Delete v2

This demonstrates constructor calls with New. The first allocation uses the default constructor, while the second passes parameters to the parameterized constructor. Constructors ensure proper object initialization.

Dynamic Strings

Strings can be dynamically allocated, though FreeBasic strings usually manage their own memory.

new_string.bas
Dim s As String Ptr = New String
*s = "Dynamic string"

Print *s

Delete s

Here we allocate a string object dynamically. Note that FreeBasic strings are reference-counted, so this is rarely needed. The example shows New works with all types, including complex ones like strings.

Pointer to Pointer

New can allocate pointers to pointers, useful for multi-dimensional arrays.

new_pointer_to_pointer.bas
Dim pp As Integer Ptr Ptr = New Integer Ptr[3]

For i As Integer = 0 To 2
    pp[i] = New Integer[4]
    For j As Integer = 0 To 3
        pp[i][j] = i * 10 + j
    Next
Next

For i As Integer = 0 To 2
    For j As Integer = 0 To 3
        Print pp[i][j]; " ";
    Next
    Print
Next

For i As Integer = 0 To 2
    Delete[] pp[i]
Next
Delete[] pp

This creates a 2D array by first allocating an array of pointers, then allocating each row. Note the nested cleanup - each row must be deleted before deleting the pointer array. This pattern is common for jagged arrays.

Placement New

FreeBasic supports placement new, which constructs an object in pre-allocated memory.

new_placement.bas
Type Test
    value As Integer
    Declare Constructor(v As Integer)
End Type

Constructor Test(v As Integer)
    this.value = v
End Constructor

Dim buffer As Byte Ptr = Allocate(SizeOf(Test))
Dim t As Test Ptr = New(buffer) Test(42)

Print "Value: "; t->value

t->~Test()
Deallocate(buffer)

Placement new constructs an object in existing memory (buffer). We must manually call the destructor (~Test()) and free the memory. This advanced technique is used in custom memory managers and embedded systems.

Best Practices

This tutorial covered the FreeBasic New keyword with practical examples showing dynamic memory allocation techniques.

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.