Revision history for KeyPgOpPlacementNew
Revision [22667]
Last edited on 2019-05-03 01:40:59 by fxm [Clarified that New [] is one-dimensional]Additions:
##**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.
Deletions:
Revision [22541]
Edited on 2018-12-28 13:18:05 by fxm [Added sentence for describing the Placement New[] operator syntax]Additions:
##**Placement New[]**## operator is the 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.
Revision [22456]
Edited on 2018-09-01 15:42:22 by fxm [Page updated while 'Operator New Overload' page creation]Additions:
Because it does not provide any dynamic memory allocation process, the ##**Placement New**## operator (unlike the ##[[KeyPgOpNew|New Expression]]## operator) does not allow any overloading by a member operator for user-defined types.
Revision [22440]
Edited on 2018-08-29 16:13:38 by fxm ['New' -> 'New Expression', 'Delete' -> 'Delete Statement']Additions:
See examples below for proper ##**Placement New**## operator usage.
Deletions:
Revision [22438]
Edited on 2018-08-29 13:57:31 by fxm ['New' -> 'New Expression', 'Delete' -> 'Delete Statement']Additions:
See examples below for proper ##**Placement New**## usage.
Deletions:
Revision [22424]
Edited on 2018-08-29 08:25:27 by fxm ['New' -> 'New Expression', 'Delete' -> 'Delete Statement']Deletions:
Additions:
It is incorrect to call ##[[KeyPgOpDelete|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.
- ##[[KeyPgOpNew|New Expression]]##
- ##[[KeyPgOpNew|New Expression]]##
Deletions:
- ##[[KeyPgOpNew|New]]##
Additions:
==={{color text="Update in progress (@fxm)." c="red"}}===
Revision [22369]
Edited on 2018-08-25 07:57:55 by fxm [Added note highlighting the risk to use PLACEMENT NEW[] in some cases]Additions:
**Note:** Using ##//pointer// = **New**(//address//)//datatype//[//count//]## may be unsafe if ##//pointer//## was declared with a type different from ##//datatype//## (for sub-type polymorphism purpose for example), because the pointer arithmetic fails to access the elements if the pointer type size is different from the size of ##//datatype//## (when using ##{{fbdoc item="keyword" value="KeyPgOpPtrIndex|Operator [] (Pointer index)"}}## or adding an offset (element number) to the pointer).
Deletions:
Revision [22367]
Edited on 2018-08-25 04:27:24 by fxm [Added note highlighting the risk to use PLACEMENT NEW[] in some cases]Additions:
**Note:** Using ##//pointer// = **New**(//address//)//datatype//[//count//]## may be unsafe if ##//pointer//## was declared with a type different from ##//datatype//## because the pointer arithmetic fails to access the elements if the pointer type size is different from the size of ##//datatype//## (when using ##{{fbdoc item="keyword" value="KeyPgOpPtrIndex|Operator [] (Pointer index)"}}## or adding an offset (element number) to the pointer).
Additions:
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor|constructor]]##s, initial values can be specified for each field (either with default initializer at data-field declaration, or with initializer list as in ##**New** //datatype// (**initializers, ..**)## if all type data-fields are numeric primitives only and without any default initializers). For types with at least one constructor, the initialize list (if any) must match an existing constructor. If no initializers are given, the default values for those types will be set.
Deletions:
Additions:
Memory is **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).
Deletions:
Additions:
Memory is **not** allocated when using the ##**Placement New**## operator. Instead, the memory at the specified ##//address//## is used (the available memory must be large enough to contain all the placement).
Deletions:
Additions:
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor|constructor]]##s, initial values can be specified for each field (either with default initializer at data-field declaration, or with initializer list as in ##**New** //datatype// (**initializers, ..**)## if all type data-fields are numeric primitives only and without any default initializers). Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
Specifying an initial value of ##[[KeyPgAny|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).
Specifying an initial value of ##[[KeyPgAny|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).
Deletions:
Specifying an initial value of ##[[KeyPgAny|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).
No Differences
Additions:
//result// = **New(//address//)** //[[DataType|datatype]]//
//result// = **New(//address//)** //[[DataType|datatype]]// ( //initializers//, ... )
//result// = **New(//address//)** //[[DataType|datatype]]//**[** //count// **]**
A pointer of type ##[[DataType|datatype]]## to the newly constructed data.
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor|constructor]]##s, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
It is incorrect to call ##[[KeyPgOpDelete|Delete]]## 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.
Specifying an initial value of ##[[KeyPgAny|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).
- Only available in the //[[CompilerOptlang|-lang fb]]// dialect.
- ##[[KeyPgDestructor|Destructor]]##
- ##[[KeyPgOpNew|New]]##
//result// = **New(//address//)** //[[DataType|datatype]]// ( //initializers//, ... )
//result// = **New(//address//)** //[[DataType|datatype]]//**[** //count// **]**
A pointer of type ##[[DataType|datatype]]## to the newly constructed data.
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor|constructor]]##s, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
It is incorrect to call ##[[KeyPgOpDelete|Delete]]## 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.
Specifying an initial value of ##[[KeyPgAny|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).
- Only available in the //[[CompilerOptlang|-lang fb]]// dialect.
- ##[[KeyPgDestructor|Destructor]]##
- ##[[KeyPgOpNew|New]]##
Deletions:
//result// = **New(//address//)** //[[DataType datatype]]// ( //initializers//, ... )
//result// = **New(//address//)** //[[DataType datatype]]//**[** //count// **]**
A pointer of type ##[[DataType datatype]]## to the newly constructed data.
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor constructor]]##s, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
It is incorrect to call ##[[KeyPgOpDelete Delete]]## 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.
Specifying an initial value of ##[[KeyPgAny 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).
- Only available in the //[[CompilerOptlang -lang fb]]// dialect.
- ##[[KeyPgDestructor Destructor]]##
- ##[[KeyPgOpNew New]]##
Revision [17088]
Edited on 2014-06-06 13:17:39 by FxMwikki [Clarified destroying of object in case of Placement New]No Differences
Revision [17087]
Edited on 2014-06-06 08:18:54 by FxMwikki [Clarified destroying of object in case of Placement New]Additions:
It is incorrect to call ##[[KeyPgOpDelete Delete]]## 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.
Type Rational
As Integer numerator, denominator
Declare Constructor ( Byval n As Integer, Byval d As Integer )
As String ratio = "/"
End Type
Constructor Rational ( Byval n As Integer, Byval d As Integer )
This.numerator = n
This.denominator = d
End Constructor
Scope
Dim As Any Ptr ap = CAllocate(Len(Rational))
Dim As Rational Ptr r = New (ap) Rational( 3, 4 )
Print ap, r
Print r->numerator & r->ratio & r->denominator
'' delete must not be used with placement new
'' destroying must be done explicitly if a destructor exists (implicitly or explicitly)
'' (in this example, the var-string member induces an implicit destructor)
Deallocate( ap )
End Scope
- ##[[KeyPgDestructor Destructor]]##
Type Rational
As Integer numerator, denominator
Declare Constructor ( Byval n As Integer, Byval d As Integer )
As String ratio = "/"
End Type
Constructor Rational ( Byval n As Integer, Byval d As Integer )
This.numerator = n
This.denominator = d
End Constructor
Scope
Dim As Any Ptr ap = CAllocate(Len(Rational))
Dim As Rational Ptr r = New (ap) Rational( 3, 4 )
Print ap, r
Print r->numerator & r->ratio & r->denominator
'' delete must not be used with placement new
'' destroying must be done explicitly if a destructor exists (implicitly or explicitly)
'' (in this example, the var-string member induces an implicit destructor)
Deallocate( ap )
End Scope
- ##[[KeyPgDestructor Destructor]]##
Deletions:
type Rational
as integer numerator, denominator
Declare Destructor ( )
end type
Destructor Rational ( )
End Destructor
scope
dim as any ptr ap = callocate(len(Rational))
dim as Rational ptr r = new (ap) Rational( 3, 4 )
print ap, r
print r->numerator & "/" & r->denominator
'' destroying must be done explicitly, because delete will automatically free the memory
'' and that isn't always okay when using placement new. ALWAYS explicitly call the destructor.
deallocate( ap )
end scope
- ##[[KeyPgOpDelete Delete]]##
Revision [17058]
Edited on 2014-04-27 07:54:51 by FxMwikki [NEW/DELETE and NEW[]/DELETE[] can now be used with the dynamic STRING data type]Deletions:
Revision [16988]
Edited on 2014-01-10 03:45:17 by FxMwikki [Added substitutive syntax for "New...Any" invalid when types with constructors]Additions:
Specifying an initial value of ##[[KeyPgAny 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).
Deletions:
Revision [16491]
Edited on 2012-12-09 07:43:56 by FxMwikki [Example: since fbc 0.25, implicit ctor/dtor is not automatically added when called as member method]Additions:
Declare Destructor ( )
Destructor Rational ( )
End Destructor
Destructor Rational ( )
End Destructor
Revision [16423]
Edited on 2012-11-08 14:07:33 by FxMwikki [Parameter "size" removed. Definition of "data type", "count" and "return value" reworded.]Additions:
name of the data type to construct.
Number of elements to construct.
A pointer of type ##[[DataType datatype]]## to the newly constructed data.
Number of elements to construct.
A pointer of type ##[[DataType datatype]]## to the newly constructed data.
Deletions:
number of bytes to allocate.
name of the data type to create.
Number of elements to allocate.
A pointer of type ##[[DataType datatype]]## to the newly allocated data.
Revision [16176]
Edited on 2012-05-15 11:29:30 by CountingPine [Formatting, combine almost-duplicated sentences]Additions:
A pointer of type ##[[DataType datatype]]## to the newly allocated data.
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor constructor]]##s, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
Specifying an initial value of ##[[KeyPgAny 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.
For simple types, like integers, an initial value can be given. For types without ##[[KeyPgConstructor constructor]]##s, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
Specifying an initial value of ##[[KeyPgAny 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.
Deletions:
For simple types, like integers, an initial value can be given. For types without constructors, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**Placement New**## as well. If no initializers are given, the default values for those types will be set.
Specifying an initial value of ##[[KeyPgAny Any]]##, as in ##new(address) datatype(any)## will not initialize the data. This is only valid on data types that do not have constructors.
Specifying an initial value of ##[[KeyPgAny Any]]##, as in ##new(address) datatype[count]{any}## will not initialize the data. This is only valid on data types that do not have constructors.
Additions:
It is incorrect to call ##[[KeyPgOpDelete Delete]]## on the address (otherwise, the memory is deallocated). The proper way is to only call the destructor (with syntax as for a member method, using member access operator).
Deletions:
Additions:
Memory is **not** allocated when using the ##**Placement New**## operator. Instead, the memory at the specified ##//address//## is used.
It is incorrect to call ##[[KeyPgOpDelete Delete]]## on the address (otherwise, the memory is deallocated). The proper way is to only call the destructor (with syntax as for a member method).
See examples below for proper //placement new// usage.
It is incorrect to call ##[[KeyPgOpDelete Delete]]## on the address (otherwise, the memory is deallocated). The proper way is to only call the destructor (with syntax as for a member method).
See examples below for proper //placement new// usage.
Deletions:
Additions:
The ##**Placement New**## operator cannot be directly used with ##[[KeyPgString String]]##, but works with ##[[KeyPgType UDT]]## containing string.
Deletions:
Additions:
The ##**Placement New**## operator cannot be used with ##[[KeyPgString String]]##, but can be used with ##[[KeyPgType UDT]]## containing string.
Deletions:
Additions:
The ##**Placement New**## operator cannot be used with ##[[KeyPgString String]]##, but can be used with ##[[KeyPgType UDT]]## containing ##[[KeyPgString String]]##.
Deletions:
Additions:
The ##**Placement New**## operator cannot be used with ##[[KeyPgString String]]##, but can be used with UDT containing ##[[KeyPgString String]]##.
Deletions:
Additions:
The ##**Placement New**## operator cannot be used with ##[[KeyPgString String]]##.
Revision [14395]
Edited on 2009-08-29 21:55:36 by CountingPine [Remove CamelCase links in "New to FreeBASIC"]Additions:
- New to ""FreeBASIC""