Why does D rely on a GC?

Maik Klein via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 18 05:27:43 PDT 2014


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?
Not sure what you mean by unqiue. It's like a reference counted 
object but with a counter of 1, meaning if it goes out of scope 
it will be deleted. (But it does not do any reference counting).

I think D also has a unique ptr. 
https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L58

Is it correct that when I create a class it will always be on the 
heap and therefore be garbage collected?

class Foo
..

So I don't have the option to put a class on the stack like in 
C++?

auto foo = Foo(); //stack
auto foo_ptr = new Foo(); // heap

If I do something like:

auto ptr = Unique(Foo);

Would the GC still be used, or would the resource be freed by the 
destructor?






More information about the Digitalmars-d mailing list