Manual memory management in D2

Vladimir Panteleev vladimir at thecybershadow.net
Tue Jul 13 11:59:30 PDT 2010


On Tue, 13 Jul 2010 21:36:02 +0300, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> Not quite. New is different because it is a primitive - it can't be  
> implemented as a function (well it could if it user placement new, but  
> we're back to square one). In contrast, delete already knows the type of  
> the object it's destroying and can call the destructor manually so it is  
> easy to implement as a regular function.

Sorry, not following you here. Why can't new be implemented as a templated  
function which takes the type of the object to create as a template  
parameter? Isn't it just allocation, copying over T.init, then calling the  
constructor with whatever arguments the user passed?

> But manual deletion has no business in the garbage collected heap.

Isn't that making the assumption that all D programs are garbage-collected?

> That currently druntime provides it is an accident caused by thecurrent  
> implementation; most GC's cannot provide efficient manual reclamation.  
> And they shouldn't anyway.

Why not? And what if I don't use the GC (just the allocation/deallocation  
aspects of it)?

> (There's a longer discussion underneath this concerning what memory  
> really is and what GCs really are and do.)

I understand your points regarding leaving deallocation to happen on a GC  
run being more efficient than manually deallocating objects individually,  
but this doesn't cover all use-cases. Also, what if I need to deallocate a  
large block of memory right now? I'd be forced to use the more verbose and  
less safe "free" functions.

-- 
Best regards,
  Vladimir                            mailto:vladimir at thecybershadow.net


More information about the Digitalmars-d mailing list