how to allocate class without gc?

Adrian Matoga via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 26 01:56:36 PST 2016


On Tuesday, 26 January 2016 at 01:09:50 UTC, Igor wrote:
> Is there any examples that shows how to properly allocate an 
> object of a class type with the new allocators and then release 
> it when desired?

There's an example of class object allocation in the 
std.experimental.allocator docs:

// Dynamically allocate a class object
static class Customer
{
     uint id = uint.max;
     this() {}
     this(uint id) { this.id = id; }
     // ...
}
Customer cust = theAllocator.make!Customer;
assert(cust.id == uint.max); // default initialized
cust = theAllocator.make!Customer(42);
assert(cust.id == 42);

To release the object (call the destructor and free the memory), 
call dispose():
theAllocator.dispose(cust);

You'll need to replace "theAllocator" with the allocator you want.


More information about the Digitalmars-d-learn mailing list