Wiki source for KeyPgOpDelete


Show raw source

{{fbdoc item="title" value="Operator Delete"}}----
Operator to delete data allocated with the ##[[KeyPgOpNew|New]]## operator

{{fbdoc item="syntax"}}##
[[KeyPgDeclare|declare]] [[KeyPgOperator|operator]] **delete** ( //buf// [[KeyPgAs|as]] [[KeyPgAny|any]] [[KeyPgPtr|ptr]] )
[[KeyPgDeclare|declare]] [[KeyPgOperator|operator]] **delete[]** ( //buf// [[KeyPgAs|as]] [[KeyPgAny|any]] [[KeyPgPtr|ptr]] )
##
{{fbdoc item="usage"}}##
**Delete** //buf//
//or//
**Delete[]** //buf//
##
{{fbdoc item="param"}}
##//buf//##
A pointer to memory that has been allocated by ##[[KeyPgOpNew|New]]## or ##[[KeyPgOpNew|New]][]## (a typed pointer must be provided in accordance to the data type to delete).

{{fbdoc item="desc"}}
##**Delete**## is used to destroy and free the memory of an object created with ##[[KeyPgOpNew|New]]##. When deleting a TYPE, its destructor will be called. ##**Delete**## should only be used with addresses returned from ##[[KeyPgOpNew|New]]##.

The array version of ##**Delete**##, ##**Delete[]**##, is used to destroy an array of objects previously created with ##[[KeyPgOpNew|New]][]##. Destructors will be called here as well.

##**Delete**## must be used with addresses returned from ##[[KeyPgOpNew|New]]##, and ##**Delete[]**## with ##[[KeyPgOpNew|New]][]##. 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**## twice on the same pointer value leads to undefined behaviour. It may be a good idea to set 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.

Calling ##**Delete**## on a null pointer induces no action.

This operator can be overloaded for user-defined types as a member ##[[KeyPgOperator|Operator]]##.

**Note:** Any operator ##**Delete[]**## (or the only overload operator ##**Delete**##) is not compatible with polymorphism, even using ##[[KeyPgOverride|Override]]## ##[[KeyPgVirtual|Virtual]]## ##[[KeyPgDestructor|Destructor]]## that may in addition induce crashing.
Instead of having to call such an operator ##**Delete([])**## on derived-type pointer, the safest way is to simply call (on base-type pointer) an overridden user ##[[KeyPgVirtual|Virtual]]## member procedure that will automatically launch the operator ##**Delete([])**## at derived-type level.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/operator/delete.bas"}}%%(freebasic)
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
%%

{{fbdoc item="filename" value="examples/manual/operator/delete2.bas"}}%%(freebasic)
' 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
%%
{{fbdoc item="lang"}}
- Only available in the //[[CompilerOptlang|-lang fb]]// dialect.

{{fbdoc item="diff"}}
- New to ""FreeBASIC""

{{fbdoc item="see"}}
- ##[[KeyPgOpNew|New]]##
- ##[[KeyPgDeallocate|Deallocate]]##

{{fbdoc item="back" value="CatPgOpMemory|Memory Operators"}}{{fbdoc item="back" value="CatPgOperators|Operators"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode