Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Memory management=== Memory is usually managed with [[garbage collection (computer science)|garbage collection]], but specific objects may be finalized immediately when they go out of scope. This is what the majority of programs and libraries written in D use. In case more control over memory layout and better performance is needed, explicit memory management is possible using the [[operator overloading|overloaded operator]] <code>new</code>, by calling [[C (programming language)|C]]'s [[malloc|malloc and free]] directly, or implementing custom allocator schemes (i.e. on stack with fallback, RAII style allocation, reference counting, shared reference counting). Garbage collection can be controlled: programmers may add and exclude memory ranges from being observed by the collector, can disable and enable the collector and force either a generational or full collection cycle.<ref>{{cite web |title=std.gc |url=http://www.digitalmars.com/d/1.0/phobos/std_gc.html |work=D Programming Language 1.0 |publisher=Digital Mars |access-date=6 July 2010}}</ref> The manual gives many examples of how to implement different highly optimized memory management schemes for when garbage collection is inadequate in a program.<ref>{{cite web |url=http://dlang.org/memory.html |title=Memory Management |publisher=Digital Mars |work=D Programming Language 2.0 |access-date=17 February 2012}}</ref> In functions, <code>struct</code> instances are by default allocated on the stack, while <code>class</code> instances by default allocated on the heap (with only reference to the class instance being on the stack). However this can be changed for classes, for example using standard library template <code>std.typecons.scoped</code>, or by using <code>new</code> for structs and assigning to a pointer instead of a value-based variable.<ref name="dlang.org">{{cite web |title=Go Your Own Way (Part One: The Stack) |url=https://dlang.org/blog/2017/07/07/go-your-own-way-part-one-the-stack/ |website=The D Blog |date=7 July 2017 |access-date=2020-05-07}}</ref> In functions, static arrays (of known size) are allocated on the stack. For dynamic arrays, one can use the <code>core.stdc.stdlib.alloca</code> function (similar to <code>alloca</code> in C), to allocate memory on the stack. The returned pointer can be used (recast) into a (typed) dynamic array, by means of a slice (however resizing array, including appending must be avoided; and for obvious reasons they must not be returned from the function).<ref name="dlang.org"/> A <code>scope</code> keyword can be used both to annotate parts of code, but also variables and classes/structs, to indicate they should be destroyed (destructor called) immediately on scope exit. Whatever the memory is deallocated also depends on implementation and class-vs-struct differences.<ref>{{cite web |title=Attributes - D Programming Language |url=https://dlang.org/spec/attribute.html#scope |website=dlang.org |access-date=2020-05-07}}</ref> <code>std.experimental.allocator</code> contains a modular and composable allocator templates, to create custom high performance allocators for special use cases.<ref>{{cite web |title=std.experimental.allocator - D Programming Language |url=https://dlang.org/phobos/std_experimental_allocator.html |website=dlang.org |access-date=2020-05-07}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)