Smart pointers instead of GC?
Sven Over
dlang at svenover.de
Tue Dec 25 06:13:20 PST 2012
Hello dear D community.
This is my very first post here. I've been intrigued with D for
quite a while, and I would love to have a project to work on in
D. I'm working as a C++ software engineer, and should be
moderately up-to-date with best practices of C++ programming.
I've read a good part of "The D Programming Language" and must
say that I *love* almost everything about D. Metaprogramming,
which quickly gets very convoluted in C++ (to say the least), is
just beautiful in D. The approach to multi-threaded programming,
and the adoption of concepts of functional programming, are all
pure gold.
The one thing that I am unsure about in D is garbage collection.
I guess this is some sort of FAQ and may have been answered
dozens of times. I apologise in advance.
The main reason I dislike garbage collection for is that
destructors of objects on the heap are not called at defined
times. Techniques like RAII
(http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization)
have become an often used pattern in my programming, and works
nicely for short lived objects (on the stack), but by means of
smart pointers (i.e. reference counting) also for long lived
objects (on the heap).
Also, garbage collection often tends to increase the memory
footprint of your software. You may be able to avoid this with
some expert knowledge about the garbage collector, but this of
course invalidates one of the main arguments in favour of GC,
which is less complexity. I'd say that if you want to write
memory- and CPU-efficient code, then you'll need a certain amount
of understanding of your memory management. Be it your manual
memory management, or the inner workings of your garbage
collector.
To cut things short, even after giving it a lot of thought I
still feel uncomfortable about garbage collection. I'm wondering
whether it is viable to use the same smart pointer techniques as
in C++: is there an implementation already of such smart
pointers? Can I switch off GC in my D programs altogether? How
much does the standard library rely on GC? I.e. would I have to
write a alternative standard library to do this?
There is one blog post I found interesting to read in this
context, which also underlines my worries:
http://3d.benjamin-thaut.de/?p=20
More information about the Digitalmars-d
mailing list