Thread overview
Explicit Class Instance Allocation
Feb 05, 2014
Mike
Feb 05, 2014
bearophile
Feb 05, 2014
Mike
Feb 05, 2014
Stanislav Blinov
Feb 05, 2014
bearophile
Feb 05, 2014
Stanislav Blinov
February 05, 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
February 05, 2014
Mike:

> class Foo
> {
>     new(size_t sz)

Also that usage of new() is deprecated.

Bye,
bearophile
February 05, 2014
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)
February 05, 2014
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?
February 05, 2014
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
February 05, 2014
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? :)