www.digitalmars.com         C & C++   DMDScript  

D - Stack memory allocation

reply Vathix <vathix dprogramming.com> writes:

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
Apr 12 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Vathix wrote:


 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/
Apr 12 2004
parent Achilleas Margaritis <axilmar b-online.gr> writes:
J Anderson wrote:
 Vathix wrote:
 

 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.
Apr 15 2004
prev sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
On Mon, 12 Apr 2004 22:00:13 -0400, Vathix <vathix dprogramming.com>
wrote:


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 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];
Apr 15 2004