Repository navigation
Operator Placement New
Home › Operators › Operator Placement New
Operator to construct an object at a specified memory address.
result = New(address) datatype
result = New(address) datatype ( initializers, ... )
result = New(address) datatype[ count ]addressinitializersdatatypecount
The Placement 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 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.
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).
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 any dynamic memory allocation 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 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 Operator [] (Pointer Index) or adding an offset (element number) to the pointer).
A pointer of type datatype to the newly constructed data.
- Only available in the -lang fb dialect.
- New to FreeBASIC
'' "placement new" example
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
'' allocate some memory to construct as a Rational
Dim As Any Ptr ap = CAllocate(Len(Rational))
'' 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->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)
r->Destructor( )
'' we explicitly allocated, so we explicitly deallocate
Deallocate( ap )
End ScopeCategory: Operators
Kind: operator
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing