Operator to delete data allocated with the
New operator
Syntax
Usage
Delete expression
or
Delete[] expression
Parameters
buf
An address of memory to free.
expression
An address of memory to free.
Description
Delete is used to destroy and free the memory of an object created with
New. When deleting a TYPE, its destructor will be called.
Delete should only be used with addresses returned from
New.
The array version of
Delete,
Delete[], is used to destroy an array of objects previously created with
New[]. Destructors will be called here as well.
Delete must be used with addresses returned from
New, and
Delete[] with
New[]. You cannot mix and match the different versions of the operators.
Example
Type Rational
As Integer numerator, denominator
End Type
Scope
' Create and initialize a Rational, and store it's address.
Dim p As Rational Ptr = New Rational(3, 4)
Print p->numerator & "/" & p->denominator
' Destroy the rational and give its memory back to the system.
Delete p
End Scope
Scope
' Allocate memory for 100 integers, store the address of the first one.
Dim p As Integer Ptr = New Integer[100]
' Assign some values to the integers in the array.
For i As Integer = 0 To 99
p[i] = i
Next
' Free the entire integer array.
Delete[] p
End Scope
Dialect Differences
Differences from QB
See also