Explicit Class Instance Allocation

Mike none at none.com
Wed Feb 5 01:59:22 PST 2014


In this article (http://dlang.org/memory.html) there is an 
example showing how one could explicitly allocate and deallocate 
an object.  However, the article seems to be sorely neglected and 
out of date ('delete' is deprecated, right?).  Could someone 
modify the example below using current best practices.

import std.c.stdlib;
import core.exception;
import core.memory : GC;

class Foo
{
     new(size_t sz)
     {
         void* p;

         p = std.c.stdlib.malloc(sz);

         if (!p)
             throw new OutOfMemoryError();

         GC.addRange(p, sz);
         return p;
     }

     delete(void* p)
     {
         if (p)
         {
             GC.removeRange(p);
             std.c.stdlib.free(p);
         }
     }
}

Thanks,
Mike


More information about the Digitalmars-d-learn mailing list