Can't I allocate at descontructor?
tsbockman
thomas.bockman at gmail.com
Fri Mar 5 21:24:08 UTC 2021
On Friday, 5 March 2021 at 21:17:24 UTC, tsbockman wrote:
> On Friday, 5 March 2021 at 21:02:08 UTC, H. S. Teoh wrote:
>> class C {...}
>>
>> import core.memory : GC;
>> C c = cast(C) GC.malloc(C.sizeof);
>> ...
> ...
> import core.memory : GC;
> C c = cast(C) GC.malloc(C.sizeof);
> ...
Also, that's not the correct way to manually allocate a class on
the heap. C.sizeof is the size of a reference to C, not an
instance of C, and we need to blit and construct the instance
before it is safe to use:
import core.memory : GC;
C c = cast(C) GC.malloc(__traits(classInstanceSize, C));
import core.lifetime : emplace;
emplace(c, anyConstructorArgsGoHere);
...
More information about the Digitalmars-d-learn
mailing list