deprecated delete and manual memory management

Daniel Gibson metalcaedes at gmail.com
Wed Apr 27 14:00:43 PDT 2011


Am 27.04.2011 22:53, schrieb so:
> On Wed, 27 Apr 2011 23:42:59 +0300, Daniel Gibson
> <metalcaedes at gmail.com> wrote:
> 
>> Am 27.04.2011 22:41, schrieb Daniel Gibson:
>>> Am 27.04.2011 22:37, schrieb so:
>>>>> It'd create template bloat and uglier syntax (expecially confusing for
>>>>> people coming from about any other popular OO language) for a really
>>>>> common, standard feature.
>>>>> These drawbacks are acceptable for custom allocation and other
>>>>> stuff the
>>>>> average user shouldn't care about, but not for an elemental feature
>>>>> like
>>>>> "new".
>>>>>
>>>>> Cheers,
>>>>> - Daniel
>>>>
>>>> For the template bloat, yes that would be a problem.
>>>> But it is not ugly! Take it back! :)
>>>>
>>>> auto a = new A;
>>>> auto a = new!A();
>>>>
>>>> auto b = new B(5);
>>>> auto b = new!B(5);
>>>>
>>>> For the confusion part, the real confusion (rather shock) awaits when
>>>> they get to the part where they see "new" but no "delete".
>>>> We could argue against this all the way, but to every single of us
>>>> "new"
>>>> and "delete" are a pair.
>>>
>>> No, in Java and C# there's no delete.
>>
>> Also, new (== creating a new Object on the heap) is a standard feature
>> in D that is needed all the time, delete (== manually destroy and
>> deallocate an Object) isn't.
> 
> As Steven also pointed out:
>> For non-garbage-collected languages, yes.  For GC languages, delete is
>> to be discouraged (that is what the GC is for)
> 
> Which makes D special, since it claims it can do both. One would expect
> it to work as it advertised, without writing a whole runtime of your own.

This claim is indeed confusing. It has been there for D1 as well and I
found it kind of disappointing that you couldn't do proper manual memory
management with Objects of *any* class but just with custom classes that
defined new() and delete()...
This is better with D2 and emplace(), enabling to write simple custom
(de)allocators - as my example has proven that can be done with about 5
lines for the allocator and 2 lines for the deallocator - that can be
used for any class. In this case however delete makes no sense because
you need to use your custom deallocator anyway.

However I think the standard use of D (both 1 and 2) is to use new and
no delete and no custom (de)allocators, even though it may have been
advertised as a core feature.

Cheers,
- Daniel


More information about the Digitalmars-d mailing list