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++?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 usingstd.conv.emplace(). The block of memory you allocate the class in can be created, for example, by usingcore.stdc.malloc(). However, note that you will have to callGC.addRange()if the class could possibly contain pointers into GC-allocated memory.