Classes and Structs, Memory management questions

dom via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 2 01:43:45 PDT 2016


from what i got Classes are always reference types and structs 
are value types in D. i am wondering why that is. for me the main 
difference between classes and structs is not how they are 
allocated, but that one has inhertiance, and the other hasn't. 
Supporting inheritance has some overhead, so at least the split 
between classes and structs makes sense to me.

I like garbage collection, but in many cases it's just 
unnecessary (like in the example below) or hurts performance on a 
bigger scale.

{
   FileReader reader = new .... // Annoy the garbage collector for 
no reason?
   auto blob = reader.read();
   delete reader;
}

Since garbage collection is a very nice feature that I wouldn't 
wanna miss for certain scenarios I think D should give us the 
opportunity to determine how an object is allocated. In the 
example above putting it on the stack is probably a good idea. 
Having a self managed reference to the heap can be good too if 
manual memory management is wanted. Or of course let the GC 
manage it ( i love it for prototyping code and also as a D 
beginner it is beneficial that i just dont need to care about 
memory management).

Could somebody explain to me if this is seen as a problem 
why/whynot and how I should address that kind of issues in my 
code?



More information about the Digitalmars-d-learn mailing list