What's the best way to avoid using GC in D? Is there a way to use classes that doesn't involve their memory being managed, or do you have to use pointers to malloc'd structs like you would in C and C++?
It was decided a long time ago that classes need to be reference types because of the slicing problem. On the other hand, D is a systems language. Therefore, using classes with manual memory management is ugly but do-able.
In D2 + Phobos, you can (unsafely) allocate a class instance on the stack using std.typecons.scoped()
. You can (again, unsafely) allocate a class in any arbitrary memory block by using std.conv.emplace()
. The block of memory you allocate the class in can be created, for example, by using core.stdc.malloc()
. However, note that you will have to call GC.addRange()
if the class could possibly contain pointers into GC-allocated memory.