Re: dynamically allocating on the stack

2018-04-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/22/18 3:17 AM, Cym13 wrote: On Sunday, 22 April 2018 at 05:29:30 UTC, Mike Franklin wrote: On Sunday, 22 April 2018 at 00:41:34 UTC, Nicholas Wilson wrote: You're not using the C library version of it, the compiler does the stack space reservation inline for you. There is no way around

Re: dynamically allocating on the stack

2018-04-22 Thread Giles Bathgate via Digitalmars-d-learn
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.

Re: dynamically allocating on the stack

2018-04-22 Thread Cym13 via Digitalmars-d-learn
On Sunday, 22 April 2018 at 05:29:30 UTC, Mike Franklin wrote: On Sunday, 22 April 2018 at 00:41:34 UTC, Nicholas Wilson wrote: You're not using the C library version of it, the compiler does the stack space reservation inline for you. There is no way around this. I'm not convinced. I did

Re: dynamically allocating on the stack

2018-04-21 Thread Mike Franklin via Digitalmars-d-learn
On Sunday, 22 April 2018 at 00:41:34 UTC, Nicholas Wilson wrote: You're not using the C library version of it, the compiler does the stack space reservation inline for you. There is no way around this. I'm not convinced. I did some no-runtime testing and eventually found the implementation

Re: dynamically allocating on the stack

2018-04-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, April 22, 2018 01:07:44 Giles Bathgate via Digitalmars-d-learn wrote: > On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer > > wrote: > > alloca is an intrinsic, and part of the language technically -- > > it has to be. > > Why does: > > scope c = new C(); //

Re: dynamically allocating on the stack

2018-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/21/18 7:47 PM, Mike Franklin wrote: On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer wrote: alloca is an intrinsic, and part of the language technically -- it has to be. From what I can tell `alloca` is only available in the platform's C standard library (actually for

Re: dynamically allocating on the stack

2018-04-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 22 April 2018 at 01:26:09 UTC, Uknown wrote: Its a special case for classes. Makes them usable without the GC. The intention was actually just to have deterministic destruction - a scope class ctor runs at the end of scope (and it used to be you could force a class to always be

Re: dynamically allocating on the stack

2018-04-21 Thread Uknown via Digitalmars-d-learn
On Sunday, 22 April 2018 at 01:07:44 UTC, Giles Bathgate wrote: On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer wrote: alloca is an intrinsic, and part of the language technically -- it has to be. Why does: scope c = new C(); // allocate c on stack scope a = new

Re: dynamically allocating on the stack

2018-04-21 Thread Giles Bathgate via Digitalmars-d-learn
On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer wrote: alloca is an intrinsic, and part of the language technically -- it has to be. Why does: scope c = new C(); // allocate c on stack scope a = new char[len]; // allocate a via gc?

Re: dynamically allocating on the stack

2018-04-21 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 21 April 2018 at 23:47:41 UTC, Mike Franklin wrote: On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer wrote: alloca is an intrinsic, and part of the language technically -- it has to be. From what I can tell `alloca` is only available in the platform's C

Re: dynamically allocating on the stack

2018-04-21 Thread Mike Franklin via Digitalmars-d-learn
On Saturday, 21 April 2018 at 19:06:52 UTC, Steven Schveighoffer wrote: alloca is an intrinsic, and part of the language technically -- it has to be. From what I can tell `alloca` is only available in the platform's C standard library (actually for Linux it appears be part of libgcc as

Re: dynamically allocating on the stack

2018-04-21 Thread Dmitry Olshansky via Digitalmars-d-learn
On Saturday, 21 April 2018 at 14:25:58 UTC, Cym13 wrote: On Saturday, 21 April 2018 at 13:54:14 UTC, H. S. Teoh wrote: On Sat, Apr 21, 2018 at 01:30:55PM +, Cym13 via Digitalmars-d-learn wrote: On Saturday, 21 April 2018 at 12:08:09 UTC, Dmitry Olshansky wrote: [...] > Unbounded

Re: dynamically allocating on the stack

2018-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/21/18 3:57 AM, Uknown wrote: On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack?  I'm looking for something roughly equivalent to the following C code. int doSomething(size_t len) {     char stackBuffer[len + 1];    

Re: dynamically allocating on the stack

2018-04-21 Thread Dmitry Olshansky via Digitalmars-d-learn
On Saturday, 21 April 2018 at 13:30:55 UTC, Cym13 wrote: On Saturday, 21 April 2018 at 12:08:09 UTC, Dmitry Olshansky wrote: On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack? I'm looking for something roughly equivalent

Re: dynamically allocating on the stack

2018-04-21 Thread Cym13 via Digitalmars-d-learn
On Saturday, 21 April 2018 at 13:30:55 UTC, Cym13 wrote: [...] Nevermind, forgot that shared libraries are put between the two.

Re: dynamically allocating on the stack

2018-04-21 Thread Cym13 via Digitalmars-d-learn
On Saturday, 21 April 2018 at 13:54:14 UTC, H. S. Teoh wrote: On Sat, Apr 21, 2018 at 01:30:55PM +, Cym13 via Digitalmars-d-learn wrote: On Saturday, 21 April 2018 at 12:08:09 UTC, Dmitry Olshansky wrote: [...] > Unbounded allocation on stack is kind of anti-pattern and a > potential DoS

Re: dynamically allocating on the stack

2018-04-21 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Apr 21, 2018 at 01:30:55PM +, Cym13 via Digitalmars-d-learn wrote: > On Saturday, 21 April 2018 at 12:08:09 UTC, Dmitry Olshansky wrote: [...] > > Unbounded allocation on stack is kind of anti-pattern and a > > potential DoS vector. > > I'm having trouble seeing how unbounded heap

Re: dynamically allocating on the stack

2018-04-21 Thread Cym13 via Digitalmars-d-learn
On Saturday, 21 April 2018 at 12:08:09 UTC, Dmitry Olshansky wrote: On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack? I'm looking for something roughly equivalent to the following C code. int doSomething(size_t len) {

Re: dynamically allocating on the stack

2018-04-21 Thread Dmitry Olshansky via Digitalmars-d-learn
On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack? I'm looking for something roughly equivalent to the following C code. int doSomething(size_t len) { char stackBuffer[len + 1]; doSomethingElse(stackBuffer); }

Re: dynamically allocating on the stack

2018-04-21 Thread Giles Bathgate via Digitalmars-d-learn
On Saturday, 21 April 2018 at 10:47:47 UTC, Timon Gehr wrote: That does not work (you are returning a dangling reference into the stack of the function that is returning). Yeah I had hoped that the pragma(inline, true) would solve that, but it dosesn't :(

Re: dynamically allocating on the stack

2018-04-21 Thread Timon Gehr via Digitalmars-d-learn
On 21.04.2018 12:08, Giles Bathgate wrote: On Saturday, 21 April 2018 at 07:57:41 UTC, Uknown wrote: The language itself doesn't have something, but you could use `alloca` I don't know if this little template function makes life easier: -- pragma(inline, true) ref T push(T)(size_t len) {

Re: dynamically allocating on the stack

2018-04-21 Thread Mike Franklin via Digitalmars-d-learn
On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack? I'm looking for something roughly equivalent to the following C code. int doSomething(size_t len) { char stackBuffer[len + 1]; doSomethingElse(stackBuffer); }

Re: dynamically allocating on the stack

2018-04-21 Thread Giles Bathgate via Digitalmars-d-learn
On Saturday, 21 April 2018 at 10:08:43 UTC, Giles Bathgate wrote: I don't know if this little template function makes life easier: Sorry, that doesn't work at all.

Re: dynamically allocating on the stack

2018-04-21 Thread Giles Bathgate via Digitalmars-d-learn
On Saturday, 21 April 2018 at 07:57:41 UTC, Uknown wrote: The language itself doesn't have something, but you could use `alloca` I don't know if this little template function makes life easier: -- pragma(inline, true) ref T push(T)(size_t len) { import core.stdc.stdlib,

Re: dynamically allocating on the stack

2018-04-21 Thread Giles Bathgate via Digitalmars-d-learn
On Saturday, 21 April 2018 at 07:57:41 UTC, Uknown wrote: The language itself doesn't have something. It would be cool if you could just do int doSomething(size_t len) { char stackBuffer = push char[len + 1]; doSomethingElse(stackBuffer); } i.e some kind of `push`

Re: dynamically allocating on the stack

2018-04-21 Thread Uknown via Digitalmars-d-learn
On Saturday, 21 April 2018 at 07:37:50 UTC, Mike Franklin wrote: Does D have some way to dynamically allocate on the stack? I'm looking for something roughly equivalent to the following C code. int doSomething(size_t len) { char stackBuffer[len + 1]; doSomethingElse(stackBuffer); }