Welcome to This Week in D! Each week, we'll summarize what's been going on in the D community and write brief advice columns to help you get the most out of the D Programming Language.
The D Programming Language is a general purpose programming language that offers modern convenience, modeling power, and native efficiency with a familiar C-style syntax.
This is the first edition of This Week in D and we're looking for help and feedback. Any suggestions for style, visual design, or content are welcome!
This Week in D is edited by Adam D. Ruppe. Contact me with any questions, comments, or contributions.
The spotlight this week is on this publication, This Week in D.
This Week in D is written in Ddoc and is about all things D, with the blessing of Walter Bright and Andrei Alexandrescu. It is a new project, and thus may still be a little rough around the edges - contributions are always welcome.
It wants to be more than just a collection of statistics and links. This Week in D should be an interesting read for casual observers and experienced D users alike, with a weekly tip column and project spotlight from me or the community to help get the word out about what you're working in on D.
This Week in D keeps its reader appraised about what's going on in D's whole ecosystem!
Want your project featured here? Prepare a link and write up a few paragraphs about it, then contact me! Your project does not have to be complete to be featured - we like works-in-progress too.
DConf 2015 will be held at Utah Valley University in Orem, Utah, from May 27, 2015 through May 29, 2015!
Hope to see you there!
See more at digitalmars.D.announce.
This week, we'll look at how to use a custom class allocator at the call site.
Class objects in D are typically allocated with the new operator:
// classes are reference types, so they must be // initialized. "MyClass c;", unlike in C++, would // leave c as null, causing a segfault when you try // to use it. // // The normal way to initialize a class is with new: MyClass c = new MyClass(args...);
There are times when you won't want to do that, however, such as when you are avoiding the garbage collector or wanting to use a custom allocator.
The alternative to new is to do it yourself: allocate the memory yourself, then call std.conv.emplace to perform a placement new in that memory. You can allocate the memory with malloc for C style heap memory, or as a static array to place it on the stack:
import std.conv : emplace; ubyte[__traits(classInstanceSize, YourClass)] buffer; YourClass obj = emplace!YourClass(buffer, ctor args...); // Destroy the object explicitly at scope exit, which will // call the destructor deterministically. scope(exit) .destroy(obj); // use obj normally here
The __traits(classInstanceSize, YourClass) portion is important: it tells you the size of a class object. YourClass.sizeof won't work because classes are reference types. It will return the size of the underlying pointer instead of the object itself!
Once your memory is allocated, you can place the class there then proceed to use it normally. The static array and scope guard will imitate C++'s class behavior in D - allocated on the stack and destroyed when it goes out of scope.
Want to learn more about the library function? Check out the emplace source code.
If you'd like to submit a tip, email me.
To learn more about D and what's happening in D: