Why does D rely on a GC?

Kagamin via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 18 08:19:10 PDT 2014


On Monday, 18 August 2014 at 12:55:52 UTC, Peter Alexander wrote:
> On Monday, 18 August 2014 at 12:06:27 UTC, Kagamin wrote:
>> On Monday, 18 August 2014 at 10:01:59 UTC, maik klein wrote:
>>> Does a GC still have advantages over heap allocations that do 
>>> not need to be reference counted such as the unique_ptr in 
>>> c++?
>>
>> Isn't unique_ptr unique? What to do when the object is 
>> non-unique?
>
> Yes, unique_ptr is unique :-)  It is not reference counted -- 
> it just destroys the owned object when it goes out of scope. 
> The near thing about unique_ptrs is that you can move them 
> around, transferring ownership.
>
> If the object is non-unique, then typically C++ programmers 
> will use shared_ptr (+ weak_ptr).
>
> I'm not sure what the status of std.typecons.Unique is. Last I 
> heard it had some issues, but I haven't tried it much myself.

So if it's not known to be unique beforehand, the safe bet is to 
use shared_ptr, if the ownership is not known beforehand, the 
safe bet is to not use weak_ptr, then you are left with all 
references annotated with spurious shared_ptr all over the place. 
It will be also easy to mess up the annotations and get dangling 
pointer.


More information about the Digitalmars-d mailing list