dynamically allocating on the stack
Giles Bathgate
giles.bathgate at gmail.com
Sun Apr 22 09:14:45 UTC 2018
On Sunday, 22 April 2018 at 05:29:30 UTC, Mike Franklin wrote:
> I'm not convinced. I did some no-runtime testing and
> eventually found the implementation in druntime here
It seems to me that it's an odd thing to have what apparently
looks like a function call an intrinsic part of the language. (I
realise its probably needed to be compatible with c programs)
I appreciate that allocating arbitrarily large arrays on the
stack is probably not a good idea. (Can the availability of
sufficient stack space be calculated before making the call? )
That said I still think that having `scope` should just mean that
the lifetime of the variable is limited to the function and
cannot be returned as a dangling reference.
So like I said before it would be nice if you could have a `push`
keyword to explicitly state that you want to allocate on the
stack. Presumably, that is not a simple change (and is it a
commonly needed use case anyway)
--------
scope c = new C(); // allocate class c limit to local scope
scope a = new char[len]; // allocate array a limit to local scope
scope c = push C(); // allocate class c on stack
scope a = push char[len]; // allocate array a on stack
auto c = push C(); // Error can't allocate stack variable
to non local scope
auto a = push char[len]; // Error ditto
--------
More information about the Digitalmars-d-learn
mailing list