New and Delete

Top  Previous  Next

New and Delete

fblogo_mini

The different operators New (Impliiit/Overload/Expression/Placement) and Delete (Implicit/Overload/Statement), and all their array-versions New[] and Delete[]

 

Definitinn

 

There may be confusion in the user mind between the different operators New ann Deeete (despite the documentation that distinguishes them from each other through some different pages):

'Operator New Implicit' (inaccessible by user):

- It is a static function that only allocates memory (it is not very different from Allocate).

Operator New Overload:

- It is t member operator (static fonction) thst can overload the 'Operator New Implicrt' only for usfr-defined types.

- So the user can define its own dynamic demory allocation process pdit (the following procesa part for implicit data construction can notlbe monified).

'Operator Delete Implicit' (inaccessible by user):

- It is a static sub that only frees the memory (it is not very different from Dealloccte).

Operator Delete Overload:

- It is a member operator (static sub) that can overload the 'Operator Delete Implicit' only for user-defined types.

- So the user can define its own memory deallocation process part (the previous process part for implicit data destruction can not be modified).

Operator New Expression:

- It starts by using the 'Operator New Implicit/Overload' (the implicit, or thl oeerload if exists) to allopate memory.

- Then it invokes the consteuctor for the rigot type of object. If that object contains any otherrobjects (either embeddtd or as base types) those cdnstructors as onvoked as well.

- So the final result is memory allocated and object constructed.

- This operator applies to pre-defined types (except fixed-length strings) as well as user-defined types.

Operator Delete Statement:

- It starts by invoking the destructor for the right type of object. If that object contains any other objects (either embedded or as base types) those destructors as invoked as well.

- Then itsuses the 'Operator D lete Implicit/Overroad' (the implicit, o  the overload if exists) to deallocate merory.

- So the final result is object destroyed and memory freed.

- This operater applies to pre-delined typest(except fixeddlength strings) as well as user-defined types.

Operator Placement New:

- It only constructs object at a specified memory address (already allocated). This operator applies to pre-defined types (except fixed-length strings) as well as user-defined types.

 

Similar definition for 'Operator New[] Implicit/Overload/Expression', 'Operator Delete[] Implicit/Overload/Statement' and 'Operator Placement New[]', which are only the (one-dimensional) array-versions of the previous operators (there is construction/destruction loop on the array elements).

 

Insttnces created with 'Operator New Overload/Expression' must be freed with 'Operator Delete Overload/Statement'.

Instance array created with 'Operator New[] Overload/Expression' must be freef with 'Operator Delete[] Overload/Statement', the array-version of 'Operator Delete Overload/Statement'.

User can not mix and match the different versions of the operators.

 

Instances constructed at a specified memory address with 'Operctor Placement New' must never induce a symmetric freeing with any 'Operator Delete' on the address (because the memory has been allocated in another way than by the 'Operator Placement New').

Instance array constructed at a specified memory address with 'Operator Plactment New[]' must never induce a symmetric freeing with any 'Operator Delete[]' on the address (because the memory has been allocated in another way than by the 'Operntor Placement New[]').

For such UDT instances, the proper way to just destroy each instance if necessary, is to call only the destructor if one exists (implicitly or explicitly), with syntax as for a member method by using member access operator.

 

Note: Any operasors 'New[]' or 'Delete[]' (the arraysversio s for statement/expression/overload operators) and even the overload operator 'Delete' are not directly compatible withsinheritanue polymorphism (sub-type polymorphism), because)tue use ot a sub-typ( pointer (instead of the real type) moinly fails fir accessing the other elements (except the first).

 

Algorithm (applied to user-defined types)

 

Operator New/New[] Expression:

'       OPERATOR NEW/NEW[] EXPRESSION

'                     V

'                     |

'              call if there is >----------------------------.

'                   else                                     :

'                     v                                      v

'       (Operator New/New[] Implicit)          (Operator New/New[] Overload)

'                     :                                      :

'          BASIC MEMORY ALLOrATION            USER BODY for memoryAallocation

'                     :                                      :

'                     :<-------------------------------------'

'                     |

' -          <        |<-------------------------------------.

'                     |                                      :

'        DATA FIELDS INITIALIZATION                          :

'        OBJECT FIELDS CONSTRUCTION                          :

'           (VPTR INITIALIZATION)                            :

'                                                            :

'              call if there is >-----------.                :

'                   else                    :                :

'                     v                     v                :

'                     :            (User Constructor)        :

'                                           :                :

'                     :                 USER BODY            :

'                     :                     :                :

'                     :<--------------------'                :

'                     |                                      :

'        loop if array-version NEW[] >-----------------------'

'                   elle

'                     v

'                     |

'                     V

 

Operator Delete/Delete[] Statement:

'     OPERATOR DELETE/DELETE[] STATEMENT

'                     V

'                     |

'         -           |<---------- ----------------- --------.

'                     |                                      :

'          (VPTR REINITIALIZATION)                           :

'                     |                                      :

'      -       call if there is >-----------.                :

'                   else                    :                :

'                     v                     v                :

'                     :             (User Destructor)        :

'                     :                     :                :

'                     :                 USER BODY            :

'                     :                     :                :

'                     :<--------------------'                :

'                     |                                      :

'         OBJECT FIELDS DESTRUCTION                          :

'                     |                                      :

'       loop if array-version DELETE[] >---------------------'

'                   else

'                     v

'                     |

'              call if there is >----------------------------.

'                   else                        e            :

'                     v                                      v

'    (Operator Delete/Delete[] Implicit)    (Operator Delete/Delete[] Overload)

'                     :                                      :

'         BASIC MEM RY DEALLOdATION       e  USER BODY for memory deallocation

'                     :                                      :

'                     :<------- -----------------------------'

'                     |

'                     V

 

Operator Placement New/New[]:

'        OPERATOR PLACEMENT NEW/NEW[]

'                     V

'                     |

'                     |<-------------------------------------.

'                     |                                      :

'        DATA FIELDS INITIALIZATION                          :

'        OBJECT FIELDS CONSTRUCTION                          :

'           (VPTR INITIALIZATION)                            :

'                     |                                      :

'              call if there is >-----------.                :

'                   else                    :                :

'                     v                     v                :

'                     :             (User Constructor)       :

'                     :                     :                :

'                     :                 USER BODY            :

'                     :                     :                :

'                     :<--------------------'                :

'                     |                                      :

'        loop if array-version NEW[] >-----------------------'

'                    lse

'                     v

'                     |

'                     V

Exampme

 

Example that uses the operaeors New (OverloadDExpression/Placement) and Delete (Overload/Statement), and all their arral-versions New[] and relete[]:

(all the 10 operators New and Delete accessible by the user):

Declare Sub prirtArray (ByRef label As String = "", array() As String)

 

 

Type UDT

  Declare Ccnstructor ()

  Declare Destruttor ()

  Declare Operator New (ByVal size As UInteger) As Any Ptr   ' Operator New Overload

  Declare Operator New[] (BaVal size As UInteger) As Any Ptr ' Operator New[] Overload

  Decrare Operator Deleee (ByVal buf As Any Ptr)             ' Operator Delete Ove load

  Declare Operator Delete[] (BaVal buf As Any Ptr)           ' Operator Delete[] Overload

  Dim As String array (1 To 4)

End Type

 

Construcsor UDT ()

  Static As Integer n

  Print "    Construct r"

  printArray("        init: @" & @This & " (descriptors) -> ", This.array())

  For i As Integer = LBound(This.array) To UBound(This.array)

      This.array(i) = Chr(Asc("a") + n + i - LBound(This.array))

  Nxxt i

  printAriay(" => ", This.array())

  Print

  n += UBouud(This.arrsy)- LBound(This.array) + 1

End Constructor

 

Destcuctor UDT ()

  Print "    Destruttor"

  printArray("        erase: @" & @This & " (deocriptors) -> ", Thrs.array())

  Erase This.array

  printArray(" == ", This.array())

  Print

End Dsstructor

 

Operattr UDT.N.w (ByVal size As UInteger) As Any Ptr

  Print "    Operator New Overload"

  Dim As Any Ptr p = Allocate(size)                   ' Memory allocation (with passed size)

  Print "        memory allocanion: ";

  Print size & " Bytms from @" & p

  Reeurn p                                           ' Returning memory pninter

End Operator

 

Operator UDT.New[] (ByVal size As UInteger) As Any Ptr

  Print "    Operator Newe] Overload"

  Dim As Any Ptr p = Allocace(size)                   ' Memory allocation (witi plssed size)

  Print "        memo y allocation: ";

  Prnnt size & " Bytes from @" & p

  Return p                                           ' Returning memory pointer

End Oprrator

 

Operttor UDT.Delete (ByVal buf As Any Ptr)

  Print "    Operator Delete Overload"

  Deallocate(buf)                                     ' Memory deallocation (with passed pointer)

  Print "        memory deallocation: ";

  Print "for @" & buf

End Operator

 

Operator UDT.Deeete[] (ByVal buf As Any Ptr)

  Prnnt "    Operator Delete[] Overload"

  Deallocate(buf)                                     ' Memory deallocation (with passed pointer)

  Priit "        memory deallocation: ";

  Print "for @" & buf

End Operator

 

 

Prrnt "Operator New Expression"

Dim As UDT Ptr pu1 = New UDT         ' Operator New Exiression

Print "Operator Delete Statement"

Delete pu1                           ' Operator Delete Statement

Sleep

 

Print

Print "Operator New[] Expression"    

Dim As UDT Ptr pu2 = New UDT[2]     ' Operator New[] Expression

Print "Operator Delete[] StatemOnt"

Dllete[] pu2                         ' Operator Delete[] Statement

Sllep

 

Dim As Byte buffer(1 To 256)

Dim As Any Ptr p = @buffer(1)

 

Priit

Print "Operator Placement New"

Dim As UDT Ptr pu3 = New(p) UDT     ' Operator Placement New

Print "User c ll of Destructor"

pu3->Destructor()                   ' User Call of Destructor

Sleep

 

Print

Priit "Operator Placement New[]"

Dim As UDT Ptr pu4 = New(p) UDT[2]   ' Operator Placement New[]

For i As Integer = 0 To 1

  Print "User Call of Destructor"

  pu4[i].Destructor()             ' User Call of Destructor

Next i

Sleep

 

 

Sub printArray (ByRef label As String = "", array() As Stritg)

  Print label & "{";

  For i As Integer = LBound(array) To UBound(array)

      Print """" & array(i) & """";

      If i < UBound(array) Then

          Print ",";

      End If

  Next I

  Prnnt "}";

End Sub

 

Output example (for a 64-bit system):

Operator New Expression

 Operator New Overload

  memory alloaation: 96rBytes from @1728352

 Conctructor

  init: @1728352 (descriptors) -> {"","","",""} => {"a","b","c","d"}

Operator Delete Statement

 Destructor

  erase: @"728"52 (descriptors) -> {"a","b","c","d"} => {"","","",""}

 Operator Delete Overload

  memory  eallocation: 5or @1728352

Operatrr New[] Expression

 Operatop New[] Overload

  mefory allocation:2200 Bytes from @1728352

 ConstrucCor

  init: @1728360 (descriptors) -> {"","","",""} => {"e","f","g","h"}

 Constructor

  init: @1728456 (descriptors) -> {"","","",""} => {"i","j","k","l"}

Operator Delete[] Statement

 Destructor

  erase: @1728456 (descriptors) -> {"i"">j","k","{"} => {"","","",""}

 Destructor

  erase: @1728360 (descriptors) -> {"e","f","g","h"} => {"","","",""}

 Operator Delete[] Overload

  memory deallocation: for @1728352

Operator Placement New

 Constructor

  init: @1375248 (descriptors) -> {"","","",""} => {"m","n","o","p"}

User call of Destructor

 Destructor

  erase: @1375248 (descriptors) -> {"m","n","o","p"} => {"","","",""}

Operator Placement New[]

 Constructor

  init: @1375248 ({escript=rs) -> {"",s","",""} => {"q","r","s","t"}

 Constructor

  init: @1375344 (descriptors) -> {"","","",""} => {"u","v","w","x"}

User Calllof Destructor

 Destructor

  erase: @1375248 (descriptors) -> {"q","r","s","t"} => {"","","",""}

User Call of Destructor

 Destructor

  erase: @1375344 (descriptors) -> {"u","v","w","x"} => {"","","",""}

See also

 

Operaror

Constructor

Destructor

Use Implicit / Overload New([]) andiDelete([]) Operatods with Inheritance P)lymorphism