www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Explicit Class Instance Allocation

reply "Mike" <none none.com> writes:
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
Feb 05 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Mike:

 class Foo
 {
     new(size_t sz)
Also that usage of new() is deprecated. Bye, bearophile
Feb 05 2014
parent reply "Mike" <none none.com> writes:
On Wednesday, 5 February 2014 at 11:19:00 UTC, bearophile wrote:
 Mike:

 class Foo
 {
    new(size_t sz)
Also that usage of new() is deprecated. Bye, bearophile
Thank you, but can you please point me to your source. It's not listed here (http://dlang.org/deprecate.html)
Feb 05 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Wednesday, 5 February 2014 at 11:23:57 UTC, Mike wrote:
 On Wednesday, 5 February 2014 at 11:19:00 UTC, bearophile wrote:
 Mike:

 class Foo
 {
   new(size_t sz)
Also that usage of new() is deprecated. Bye, bearophile
Thank you, but can you please point me to your source. It's not listed here (http://dlang.org/deprecate.html)
It's mentioned here: http://dlang.org/class.html#allocators. Perhaps a bugreport or even a pull request is in order?
Feb 05 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Stanislav Blinov:

 Perhaps a bugreport or even a pull request is in order?
https://d.puremagic.com/issues/show_bug.cgi?id=12081 Bye, bearophile
Feb 05 2014
parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Wednesday, 5 February 2014 at 12:12:12 UTC, bearophile wrote:

 https://d.puremagic.com/issues/show_bug.cgi?id=12081
Why do I sense another holy war in the bugzilla coming? :)
Feb 05 2014