操作符 Delete
 
删除使用New操作符赋值的数据

语法

Declare Operator Delete ( buf As Any Ptr )
Declare Operator delete[] ( buf As Any Ptr )

用法

Delete buf
or
Delete[] buf

参数

buf
NewNew[]赋值的指向内存的指针(必须根据要删除的数据类型提供类型化的指针)。

说明

Delete用于销毁和释放使用New创建的对象的内存。删除TYPE时,它的析构函数将被调用。Delete只能与New返回的地址一起使用。

阵列版本DeleteDelete[]用于销毁先前使用New[]创建的对象数组。破坏者也将在这里被称为。

Delete必须与NewDelete[]New[]返回的地址一起使用。您不能混合和匹配不同版本的运算符。

内存被删除后,buf 指针将指向无效内存。在同一个指针值上调用Delete两次导致未定义的行为。设置buf pointer to null (0), 为了防止后来的代码使用它意外,因为空指针解引用更容易找到和调试。

在空指针上调用 删除 不会导致任何操作。

例子

Type Rational
    As Integer numerator, denominator
End Type

' Create and initialize a Rational, and store its 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

' Set the pointer to null to guard against future accesses
p = 0


' 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

' Set the pointer to null to guard against future accesses
p = 0

方言差异

  • Only available in the -lang fb 方言可能是个好主意。

与QB差别

  • 新的FreeBASIC

参考