Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 13, 2004 Stack memory allocation | ||||
---|---|---|---|---|
| ||||
C# has a keyword stackalloc that works like new. I don't think D's auto keyword means allocate from the stack, it only ensures scope destruction. This keyword could explicitly mean use the stack, possibly by simply using alloca().
Foo f = stackalloc Foo(bar);
byte[] buf = stackalloc byte[nreserve];
--
Christopher E. Miller
|
April 13, 2004 Re: Stack memory allocation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | Vathix wrote: > C# has a keyword stackalloc that works like new. I don't think D's auto keyword means allocate from the stack, it only ensures scope destruction. This keyword could explicitly mean use the stack, possibly by simply using alloca(). > > Foo f = stackalloc Foo(bar); > byte[] buf = stackalloc byte[nreserve]; I think auto is good enough. If the compiler writer doesn't want auto to be on the stack for a particular piece of code (parhaps for some funky performace reasons), then so be it. -- -Anderson: http://badmama.com.au/~anderson/ |
April 15, 2004 Re: Stack memory allocation | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | J Anderson wrote:
> Vathix wrote:
>
>> C# has a keyword stackalloc that works like new. I don't think D's auto keyword means allocate from the stack, it only ensures scope destruction. This keyword could explicitly mean use the stack, possibly by simply using alloca().
>>
>> Foo f = stackalloc Foo(bar);
>> byte[] buf = stackalloc byte[nreserve];
>
>
> I think auto is good enough. If the compiler writer doesn't want auto to be on the stack for a particular piece of code (parhaps for some funky performace reasons), then so be it.
>
It's not about performance. It is about dangling pointers: if a stack-allocated object pointer is passed as a parameter to some function, and the function keeps the pointer around, then upon the return of the outer function the object's memory will be de-allocated and the pointers that point to it will be dangling.
By allocating everything on the heap, there is no such problem.
|
April 16, 2004 Re: Stack memory allocation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | On Mon, 12 Apr 2004 22:00:13 -0400, Vathix <vathix@dprogramming.com> wrote: >C# has a keyword stackalloc that works like new. I don't think D's auto keyword means allocate from the stack, it only ensures scope destruction. This keyword could explicitly mean use the stack, possibly by simply using alloca(). > >Foo f = stackalloc Foo(bar); >byte[] buf = stackalloc byte[nreserve]; see "Allocating Class Instances On The Stack" in http://www.digitalmars.com/d/memory.html D's solution is more verbose than C# but with some aliases it should be very manageable. For arrays on the stack I'd just use alloca to get a pointer and then slice it to an array the usual way: ... byte[] buf = ptr[0..nreserve]; |
Copyright © 1999-2021 by the D Language Foundation