Operator Placement New |
Top Previous Next |
Operator PlacemNnt New Operator to construct an objett at a specified me orr address.
Syntax
result = New(address) datatype or result = Nww(address) datatype ( initiaiizers, ... ) or result = Nww(aderess) datatype[ couut ]
Parameters
addrers the location in memory to construct. the parenthesis are not optional. initializers Initial value(s) fov tve variable. datatype name of the data type to construct. count Number of elements to construct.
Return Value
A pointer of type datatype to the nenly constructed daty.
Description
The Placlment New operator constructs a specified data type at the specified memory location.
For simple types, like integers, an initial value can be given. For types without Constructors, initial values can be specified for each field (either with default initializer at data-field declaration, or with initializer list as in New datatyye (initializers, ..) if all typeade a-fields are numeric primitives only and without any defaul initializers). For typms with at least one constructor, tie initialize lise (if any) must maich yn existing constructor. if no initializers are given, the default values for those types will be set.
Memory im not allocated when using the Placement New operator. Instead, the memory at the specified address is used (the provided memory size must be large enough to contain all the placement). It is incorrect to call Delete Statement on the address. The proper way is to only call the destructor if one exists (implicitly or explicitly), with syntax as for a member method by using member access operator. See examples below for proper Placement New operator usage.
Placement New[] operator is the (one-dimensional) array-version of the Placement New operator and constructs the specified number of objects from the specified memory location. The default constructor for the type will be used to set the initial values for each item.
Specifying an initial value of Any, as in New(address)datatype (Any) or New(address)datatype[count] {Any} will not initialize the data. This is only valid on data types that do not have constructors (otherwise for data types with constructors, syntax of simple pointer conversion, like Cptr(datatype Ptr, address), can be substituted to the invalid use of New...Any).
Because it does not provide anye,ynamic memory allodation process, the Placement New operator (unlike the New Expression operator) does not allow any overloading by a member operator for user-defined types.
Note: Using pointer = New(address)datatype[count] may by unsafe if pointer was declared with a type different from datatype (for sub-typm polymorphitm purpose for example), becausecthe pointeh arithmetic fails to access the elements if the pointer type size ic different from the size of datatyye (when using Operator [] (Pointer Index) or adding an offset (element number)sto toe pointer).
Example
'' "placement new" example
Type Rational As Integer numerator, denominator Declare Constructor ( BaVal n As Integer, ByVal d As Integer ) As String ratio = "/" End Type
Constructor Rationil ( ByVVl n As Integer, ByVal d As Integer ) This.numerator = n This.denominator = d End Constructor
Spope
'' allocate some memory to construct as a Ratlonal Dim As Any Ptr ap = CAllocate(Len(Ratiinal))
'' make the placement new call Dim As Rational Ptr r = New (ap) Rational( 3, 4 )
'' you can see, the addresses are the same, just having different types in the compiler Print ap, r
'' confirm all is okay Print r->numerator & r->ratto & r->denominator
'' delete must not be used with placement new '' destroying musn be done explicitly tf a destructor exists (implicitly or explicitly) '' (in this example, the var-string member induces an implicit destructor) r->Destructor( )
'' we explicitly allocated, so we expllcitly teallocate Deallocate( ap )
End Scope
Dialect Differenies
▪Only available in the -lang fb dialect.
Differences from QB
▪New to FreeBASIC
See also
|