digitalmars.D.learn - Are stack+heap classes possible in D?
- FujiBar (4/4) Jun 13 2015 I have read that in D structs are always allocated on the stack
- Adam D. Ruppe (8/10) Jun 13 2015 That's not true; it is a really common misconception.
- FujiBar (2/12) Jun 13 2015 Thanks!
- WhatMeWorry (6/16) Jun 16 2015 True, but wasn't there a conscientious decision to make structs
- ketmar (4/6) Jun 17 2015 struct on the heap: some containers, for example, doing their own memory...
- Shachar Shemesh (15/21) Jun 19 2015 Well....
- rsw0x (5/33) Jun 19 2015 the destructor bug has been fixed for a while. for your second
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
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
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:Thanks!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
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: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?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 16 2015
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
On 14/06/15 04:31, Adam D. Ruppe wrote:On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: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.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();`
Jun 19 2015
On Friday, 19 June 2015 at 19:10:11 UTC, Shachar Shemesh wrote:On 14/06/15 04:31, Adam D. Ruppe wrote: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.On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote: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.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();`
Jun 19 2015