Calling destroy on struct pointer

Radu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 25 07:21:56 PST 2017


On Saturday, 25 February 2017 at 13:18:21 UTC, Moritz Maxeiner 
wrote:
> On Saturday, 25 February 2017 at 13:14:24 UTC, Moritz Maxeiner 
> wrote:
>> ---
>> struct A {}
>>
>> auto a = cast (A*) malloc(A.sizeof); // Allocate
>> emplace(a, 42);                      // Construct
>>
>> destroy(a);                          // Destruct
>> free(a);                             // Deallocate
>> ---
>
> Sorry for double posting, I failed at copy-paste, here's the 
> correct example:
>
> ---
> struct A { int i; }
>
> auto a = cast (A*) malloc(A.sizeof); // Allocate
> emplace(a, 42);                      // Construct
>
> destroy(a);                          // Destruct
> free(a);                             // Deallocate
> ---

The correct way of doing it using deref would to look like:

struct A { int i; }

auto a = cast (A*) malloc(A.sizeof); // Allocate
emplace(a, 42);                      // Construct
destroy(*a);                          // Destruct A
free(a);                             // Deallocate
destroy(a);                          // Destruct A*
assert(a is null);



More information about the Digitalmars-d-learn mailing list