dynamically allocating on the stack

2018-04-21 Thread Mike Franklin via Digitalmars-d-learn
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); } Thanks, Mike

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); }

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 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: Getting the overload set of a template

2018-04-21 Thread Alex via Digitalmars-d-learn
On Thursday, 19 April 2018 at 17:55:47 UTC, Simen Kjærås wrote: Your first example defines two templates (which are overloads of the same name), the second only one. There's no ambiguity there. So, do you mean, that the constraint belongs to the interface of a template?

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 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) {

How to use std.meta.Filter?

2018-04-21 Thread Dr.No via Digitalmars-d-learn
import std.meta : Filter; enum isNotReservedSymbol(string name) = name != "none" && name != "lastToken"; enum string[] members = staticMembers!Token; static foreach(member; Filter!(isNotReservedSymbol, members)) {{ This return the error: Error: template instance

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 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 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 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 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 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 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 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 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: How to use std.meta.Filter?

2018-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/21/18 1:46 PM, Dr.No wrote: On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis wrote: On Saturday, April 21, 2018 16:05:22 Dr.No via Digitalmars-d-learn wrote: import std.meta : Filter; enum isNotReservedSymbol(string name) = name != "none" && name != "lastToken"; enum string[]

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 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: How to use std.meta.Filter?

2018-04-21 Thread Uknown via Digitalmars-d-learn
On Saturday, 21 April 2018 at 17:46:05 UTC, Dr.No wrote: On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis wrote: On Saturday, April 21, 2018 16:05:22 Dr.No via Digitalmars-d-learn wrote: import std.meta : Filter; enum isNotReservedSymbol(string name) = name != "none" && name !=

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?

ldc flags for beginner

2018-04-21 Thread fevasu via Digitalmars-d-learn
what flags to use so that the intermediate .o files are discared by ldc and only a.out is written to disk

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 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 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: How to use std.meta.Filter?

2018-04-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, April 21, 2018 16:05:22 Dr.No via Digitalmars-d-learn wrote: > import std.meta : Filter; > enum isNotReservedSymbol(string name) = name != "none" && name != > "lastToken"; > enum string[] members = staticMembers!Token; > static foreach(member; Filter!(isNotReservedSymbol, members)) >

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: How to use std.meta.Filter?

2018-04-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, April 21, 2018 17:46:05 Dr.No via Digitalmars-d-learn wrote: > On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis > > wrote: > > On Saturday, April 21, 2018 16:05:22 Dr.No via > > > > Digitalmars-d-learn wrote: > >> import std.meta : Filter; > >> enum

Advice on : rmdirRecurse and setAttributes in Windows

2018-04-21 Thread Vino via Digitalmars-d-learn
Hi All, The function rmdirRecurse does not work in Windows if the file has the READ ONLY permission, so to over come this issue, i have written the below function to set the permission on file and folder using the function "setAttributes" , so can any one advice me whether the below code

Re: Getting the overload set of a template

2018-04-21 Thread Simen Kjærås via Digitalmars-d-learn
On Saturday, 21 April 2018 at 11:23:33 UTC, Alex wrote: On Thursday, 19 April 2018 at 17:55:47 UTC, Simen Kjærås wrote: Your first example defines two templates (which are overloads of the same name), the second only one. There's no ambiguity there. So, do you mean, that the constraint

Re: Getting the overload set of a template

2018-04-21 Thread Alex via Digitalmars-d-learn
On Saturday, 21 April 2018 at 19:51:05 UTC, Simen Kjærås wrote: On Saturday, 21 April 2018 at 11:23:33 UTC, Alex wrote: So, do you mean, that the constraint belongs to the interface of a template? Not necessarily - it depends on what you want to achieve. The only thing I mean is that the

Re: How to use std.meta.Filter?

2018-04-21 Thread Dr.No via Digitalmars-d-learn
On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis wrote: On Saturday, April 21, 2018 16:05:22 Dr.No via Digitalmars-d-learn wrote: import std.meta : Filter; enum isNotReservedSymbol(string name) = name != "none" && name != "lastToken"; enum string[] members = staticMembers!Token;

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 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