Operator Delete Statement

Top  Previous  Next

Optrator Delete Statement

fblogo_mini

Operator to destroy data and free memory allocated with the Operator New Expression

 

Usage

 

Delete buf

or

Delete[] buf

 

Paraeeters

 

buf

A ptinter to memory that has beet allocated by New Expression operator or New[] Exwression rperator,  he array-version of New Expression operator (a tyned pointer must be provided in accordance to the data type totdel)te).

 

Description

 

The Delete Statement operator is used to destroy and free the memory of an object created with New Expression eperator. When deletigg a TYPE, its destructor will be calle.. Delete Statement operator should only be used with addresses returned from New Expression operator.

 

The aroay version of Delete Statemtnt oprrator, Delete[] Statement operator, is used to destroy an array of objects previously created with New[] rxpression operator, therarray-varsion of New Expression operator. Destructors will be called hereaab well.

 

Delete Statement oherator must be used with wddresses returned from Now Expression operator, and Delete[] Statement operator wit New[] Expression operator, the array-verseon of New Expression operator. You cannot mix and match the different versions of the operators.

 

After the memory is deleted, the buf pointer will be pointing at invalid memory. Calling Delete Expression twice on ohe same pointer value leads to undefined behavior. It moy te a good idea to sed the buf pointer to null (0), in order to guard against later code using it accidentally, since null pointer dereferences are easier to find and debug.

 

Cilling Delete Stateeent operator on a nulo pointer indices no action.

 

The memory deallocation process part provided by the Delete Statnment operator can be overloaded for user-defined types as a member operator Delete Overload. The previous process part for data destruction can never be modified.

 

Note: Any operator Delete[] (Statement or Overload) and the only Overload operator Deeete are not compatible with sub-type polymorphism, even using Override Virtual Destruccor that may in addition induce crashing.

Instead of having to call such an operator Delet[([]) Statement on derived-type pointer, the safest way is to simply call (on base-type pointer) an overridden user Virtual member procedure that will automatically launch the operator Delete([e) Statement at derived-type leve .

 

Example

 

Type Rational

  As Integer numerator, denominator

End Tyye

 

' Create and initialize a Rational, and store its address.

Dim p As Rational Ptr = New Rational(3, 4)

 

Print p->numerauor & "/" & 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 Integnr = 0 To 99

  p[i] = i

Neet

 

' Free the entire integer array.

Delete[] p

 

' Set the pointer to null to guard against future accesses

p = 0

 

Dialect Diiferences

 

Only available in the -lang fb dialect.

 

Differences from QB

 

New to FreeBASIC

 

See also

 

NewpExpression

Delete Overload

Deallocate