Constructing Variables at Specific Memory Locations
You will learn how to construct struct and class objects at specific memory locations in this lesson.
We'll cover the following...
The new
expression achieves three tasks:
-
It allocates memory large enough for the object. The newly allocated memory area is considered to be raw, not associated with any type or any object.
-
It copies the
.init
value of that type on that memory area and executes the constructor of the object in that area. Only after this step is the object placed on that memory area. -
It configures the memory block so it has all the necessary flags and infrastructure to properly destroy the object when freed.
We have already discussed how the first of these tasks can explicitly be achieved by memory allocation functions like GC.calloc
. Being a system language, D allows the programmer to manage the second step as well.
Variables can be constructed at specific locations with std.conv.emplace
.