www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Stack allocated arrays

reply "Namespace" <rswhite4 googlemail.com> writes:
Code: http://dpaste.dzfl.pl/6ae9f91a
Can someone explain me these results (switch the version 
statement from 'none' to 'all' in 'reserve')?
Is it possible that I blown the stack frame? If so: why and can 
my code/idea work?
Nov 17 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 Code: http://dpaste.dzfl.pl/6ae9f91a
I think you can't use alloca() lie this, buf vanishes when the constructor ends: this(const size_t len)(ref T[len] arr) { this._buf = cast(T*) alloca(len * T.sizeof); for (size_t i = 0; i < len; ++i) { this._buf[i] = arr[i]; } this._length = len; } Bye, bearophile
Nov 17 2012
prev sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Sunday, 18 November 2012 at 01:55:48 UTC, Namespace wrote:
 Code: http://dpaste.dzfl.pl/6ae9f91a
 Can someone explain me these results (switch the version 
 statement from 'none' to 'all' in 'reserve')?
 Is it possible that I blown the stack frame? If so: why and can 
 my code/idea work?
Maybe the array is allocated in the frame of the constructor which is lost afterwards.
Nov 18 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Tobias Pankrath:

 Maybe the array is allocated in the frame of the constructor 
 which is lost afterwards.
That's probably right, you can't use alloca that way (It seems my answer yesterday got lost). Bye, bearophile
Nov 18 2012
parent reply Tobias Pankrath <lists pankrath.net> writes:
On 18.11.2012 16:21, bearophile wrote:
 Tobias Pankrath:

 Maybe the array is allocated in the frame of the constructor which is
 lost afterwards.
That's probably right, you can't use alloca that way (It seems my answer yesterday got lost). Bye, bearophile
I just took a look. It's in my mailbox but does not show up in the web interface.
Nov 18 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
Ok, understood.
I hoped that I could create a better interface.
Nov 18 2012
parent reply Tobias Pankrath <lists pankrath.net> writes:
On 18.11.2012 16:46, Namespace wrote:
 Ok, understood.
 I hoped that I could create a better interface.
I think a good interface would be a generic array class/struct that can be parameterized to use a stack allocator.
Nov 18 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 18 November 2012 at 16:01:10 UTC, Tobias Pankrath 
wrote:
 On 18.11.2012 16:46, Namespace wrote:
 Ok, understood.
 I hoped that I could create a better interface.
I think a good interface would be a generic array class/struct that can be parameterized to use a stack allocator.
The best would be if my code would work. I wish some syntax like: int len = 42; int[len] arr = stackalloc!(int)(len);
Nov 18 2012