digitalmars.D - How does D allocate objects?
- Tomás Rossi (8/8) Oct 29 2005 Coming from previous discussions about the GC and reading later answers,...
- Hasan Aljudy (8/21) Oct 29 2005 As far as I understand, Objects in D are always references, never values...
- Jarrett Billingsley (15/17) Oct 30 2005 You can override this behavior by writing your own class allocator (cust...
- Tomás Rossi (4/21) Oct 30 2005 I guess that would be a good example.
- Mike Parker (36/42) Oct 30 2005 Struct instances can be alloctated statically:
- Tomás Rossi (3/45) Oct 30 2005 Ok thank you very much!, you've cleared my mind.
- Dave (3/11) Oct 30 2005 Here's a number of ways to allocate class objects:
Coming from previous discussions about the GC and reading later answers, i think i'm a little confused: 1) Objects in D are ONLY heap-allocated and never stack-allocated? 2) Can you create static objects in D? If the anwer to (2) is NO then i suppose something like this is forbidden: MyObject myobj_instance(initializer params...); Thanks Tom
Oct 29 2005
Tomás Rossi wrote:Coming from previous discussions about the GC and reading later answers, i think i'm a little confused: 1) Objects in D are ONLY heap-allocated and never stack-allocated? 2) Can you create static objects in D? If the anwer to (2) is NO then i suppose something like this is forbidden: MyObject myobj_instance(initializer params...); Thanks TomAs far as I understand, Objects in D are always references, never values. There are certain circumistances where objects maybe allocated on the stack, but even then, the "new" keyword must be used! And I suspect that these circumistances are not guaranteed to create the objects on the stack, i.e. it's the compiler's dicision (implementation dependet). I only know one such circumistance: auto (raii) objects that don't have a destructor.
Oct 29 2005
"Tomás Rossi" <Tomás_member pathlink.com> wrote in message news:dk1buh$1jcb$1 digitaldaemon.com...1) Objects in D are ONLY heap-allocated and never stack-allocated?You can override this behavior by writing your own class allocator (custom new operator per-class). It's in the docs: http://www.digitalmars.com/d/memory.html#stackclass2) Can you create static objects in D?You mean something like class A { static A someA; static this() { someA = new A(); } } That? or have I missed your point?
Oct 30 2005
In article <dk3dso$pf1$1 digitaldaemon.com>, Jarrett Billingsley says..."Tomás Rossi" <Tomás_member pathlink.com> wrote in message news:dk1buh$1jcb$1 digitaldaemon.com...I guess that would be a good example. PS: The example i gave was about question 1). Tom1) Objects in D are ONLY heap-allocated and never stack-allocated?You can override this behavior by writing your own class allocator (custom new operator per-class). It's in the docs: http://www.digitalmars.com/d/memory.html#stackclass2) Can you create static objects in D?You mean something like class A { static A someA; static this() { someA = new A(); } } That? or have I missed your point?
Oct 30 2005
Tomás Rossi wrote:1) Objects in D are ONLY heap-allocated and never stack-allocated?Struct instances can be alloctated statically: SomeStruct s; s.somefield = 1; Class instances are heap allocated and can only be newed: MyClass c = new MyClass(); // <- OK MyClass c(); // <-- not OK2) Can you create static objects in D? If the anwer to (2) is NO then i suppose something like this is forbidden: MyObject myobj_instance(initializer params...);If you need to do this in a method/function for some class instance that you wish to be auto-destructed when it goes out of scope, use the auto keyword: void MyFunc() { // c will be destructed when it goes out of scope auto MyClass c = new MyClass(); } If there's something you wish to statically allocate during app startup, use module constructors. module mypackage.mymodule; class MyClass { public this() { doSomething(); } } // 'static' instance MyClass c; // module constructor static this() { c = new MyClass(); } // you can also have module destructors static ~this() { }
Oct 30 2005
In article <dk3oob$1c5c$1 digitaldaemon.com>, Mike Parker says...Tomás Rossi wrote:Ok thank you very much!, you've cleared my mind. Tom1) Objects in D are ONLY heap-allocated and never stack-allocated?Struct instances can be alloctated statically: SomeStruct s; s.somefield = 1; Class instances are heap allocated and can only be newed: MyClass c = new MyClass(); // <- OK MyClass c(); // <-- not OK2) Can you create static objects in D? If the anwer to (2) is NO then i suppose something like this is forbidden: MyObject myobj_instance(initializer params...);If you need to do this in a method/function for some class instance that you wish to be auto-destructed when it goes out of scope, use the auto keyword: void MyFunc() { // c will be destructed when it goes out of scope auto MyClass c = new MyClass(); } If there's something you wish to statically allocate during app startup, use module constructors. module mypackage.mymodule; class MyClass { public this() { doSomething(); } } // 'static' instance MyClass c; // module constructor static this() { c = new MyClass(); } // you can also have module destructors static ~this() { }
Oct 30 2005
In article <dk1buh$1jcb$1 digitaldaemon.com>, Tomás Rossi says...Coming from previous discussions about the GC and reading later answers, i think i'm a little confused: 1) Objects in D are ONLY heap-allocated and never stack-allocated? 2) Can you create static objects in D? If the anwer to (2) is NO then i suppose something like this is forbidden: MyObject myobj_instance(initializer params...); Thanks TomHere's a number of ways to allocate class objects: http://digitalmars.com/d/memory.html
Oct 30 2005