www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are stack+heap classes possible in D?

reply "FujiBar" <no spam.com> writes:
I have read that in D structs are always allocated on the stack 
while classes are always allocated on the heap. Well, I often 
have classes where I want some instances on the stack, some on 
the heap. So.. what to do?
Jun 13 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
 I have read that in D structs are always allocated on the stack 
 while classes are always allocated on the heap.
That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there.
Jun 13 2015
next sibling parent "FujiBar" <no spam.com> writes:
On Sunday, 14 June 2015 at 01:31:25 UTC, Adam D. Ruppe wrote:
 On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
 I have read that in D structs are always allocated on the 
 stack while classes are always allocated on the heap.
That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there.
Thanks!
Jun 13 2015
prev sibling next sibling parent reply "WhatMeWorry" <kc_heaser yahoo.com> writes:
On Sunday, 14 June 2015 at 01:31:25 UTC, Adam D. Ruppe wrote:
 On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
 I have read that in D structs are always allocated on the 
 stack while classes are always allocated on the heap.
That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();` Putting a class on the stack is a bit trickier, but the standard library provides a helper to do it: http://dlang.org/phobos/std_typecons.html#scoped follow the examples given there.
True, but wasn't there a conscientious decision to make structs more amenable to value semantics while classes are more conducive to reference semantics. I guess the question would be why would one want a struct on the heap and a class on the stack? Performance reasons?
Jun 16 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 17 Jun 2015 06:02:46 +0000, WhatMeWorry wrote:

 I guess the question would be why would one want a struct on the heap
 and a class on the stack?  Performance reasons?
struct on the heap: some containers, for example, doing their own memory=20 management. class on the stack: guaranteed destruction when leaving a scope.=
Jun 17 2015
prev sibling parent reply Shachar Shemesh <shachar weka.io> writes:
On 14/06/15 04:31, Adam D. Ruppe wrote:
 On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
 I have read that in D structs are always allocated on the stack while
 classes are always allocated on the heap.
That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();`
Well.... Yeah. You would get a reference to a struct. The struct will be on the heap. In that narrow sense, you are right that it is possible. However, this does not behave like a normal struct. In particular, when will the destructor be called? (answer: never, not even before the memory is collected). So, no, I think D experts should avoid telling newbies it is okay to just "new struct foo".[1] Shachar 1 - The counter argument is, of course, that struct destructors should not be counted upon to do anything useful anyways, as they are far from guaranteed to run even in situations where one would expect them to. This just relates to another area where D skirts truth in advertising when people say that D supports RAII.
Jun 19 2015
parent "rsw0x" <anonymous anonymous.com> writes:
On Friday, 19 June 2015 at 19:10:11 UTC, Shachar Shemesh wrote:
 On 14/06/15 04:31, Adam D. Ruppe wrote:
 On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
 I have read that in D structs are always allocated on the 
 stack while
 classes are always allocated on the heap.
That's not true; it is a really common misconception. Putting a struct on the heap is trivial and built into the language: `S* s = new S();`
Well.... Yeah. You would get a reference to a struct. The struct will be on the heap. In that narrow sense, you are right that it is possible. However, this does not behave like a normal struct. In particular, when will the destructor be called? (answer: never, not even before the memory is collected). So, no, I think D experts should avoid telling newbies it is okay to just "new struct foo".[1] Shachar 1 - The counter argument is, of course, that struct destructors should not be counted upon to do anything useful anyways, as they are far from guaranteed to run even in situations where one would expect them to. This just relates to another area where D skirts truth in advertising when people say that D supports RAII.
the destructor bug has been fixed for a while. for your second point, the issue is that D doesn't separate destructors from finalizers and it feels like it was designed by someone with little knowledge in low level memory management honestly.
Jun 19 2015