Thread overview
Are stack+heap classes possible in D?
Jun 14, 2015
FujiBar
Jun 14, 2015
Adam D. Ruppe
Jun 14, 2015
FujiBar
Jun 17, 2015
WhatMeWorry
Jun 17, 2015
ketmar
Jun 19, 2015
Shachar Shemesh
Jun 19, 2015
rsw0x
June 14, 2015
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?
June 14, 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.
June 14, 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:
>> 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!
June 17, 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:
>> 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?
June 17, 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 management.

class on the stack: guaranteed destruction when leaving a scope.

June 19, 2015
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.
June 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:
>> 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.