digitalmars.D.learn - Creating fixed array on stack
- Andrey (5/7) Jan 11 2019 In D the "count" is part of type and must be known at CT but in
- Dgame (17/24) Jan 11 2019 You could try alloca:
- Johan Engelen (6/15) Jan 11 2019 Indeed, but make sure you read up on the caveats of using alloca.
- Andrey (5/30) Jan 12 2019 Thank you.
- Adam D. Ruppe (3/6) Jan 12 2019 alloca is a magical function; a compiler intrinsic that works the
- Seb (3/9) Jan 12 2019 Yup, specifically (at least for DMD) it is defined here:
Hi, In C++ you can create a fixed array on stack:int count = getCount(); int myarray[count];In D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap.
Jan 11 2019
On Friday, 11 January 2019 at 14:46:36 UTC, Andrey wrote:Hi, In C++ you can create a fixed array on stack:You could try alloca: ---- import core.stdc.stdlib: alloca; pragma(inline, true) auto stack(T, alias len)(void* p = alloca(T.sizeof * len)) { return (cast(T*) p)[0 .. len] = T.init; } void main() { import std.stdio: writeln; int size = 42; auto a = stack!(int, size); writeln(a); a[] = 2; writeln(a); } ----int count = getCount(); int myarray[count];In D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap.
Jan 11 2019
On Friday, 11 January 2019 at 15:23:08 UTC, Dgame wrote:On Friday, 11 January 2019 at 14:46:36 UTC, Andrey wrote:Small correction: this is valid in C, but not in C++.Hi, In C++ you can create a fixed array on stack:int count = getCount(); int myarray[count];Indeed, but make sure you read up on the caveats of using alloca. I believe all use cases of VLA/alloca have better alternative implementation options. -JohanIn D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap.You could try alloca:
Jan 11 2019
On Friday, 11 January 2019 at 15:23:08 UTC, Dgame wrote:On Friday, 11 January 2019 at 14:46:36 UTC, Andrey wrote:Thank you. But this requires using of C function "alloca". I think this cause some RT overhead (and in output asm code) in comparison with C++ variant. Or I'm not right?Hi, In C++ you can create a fixed array on stack:You could try alloca: ---- import core.stdc.stdlib: alloca; pragma(inline, true) auto stack(T, alias len)(void* p = alloca(T.sizeof * len)) { return (cast(T*) p)[0 .. len] = T.init; } void main() { import std.stdio: writeln; int size = 42; auto a = stack!(int, size); writeln(a); a[] = 2; writeln(a); } ----int count = getCount(); int myarray[count];In D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap.
Jan 12 2019
On Saturday, 12 January 2019 at 08:10:47 UTC, Andrey wrote:But this requires using of C function "alloca". I think this cause some RT overhead (and in output asm code) in comparison with C++ variant. Or I'm not right?alloca is a magical function; a compiler intrinsic that works the same way as the C built-in.
Jan 12 2019
On Saturday, 12 January 2019 at 13:17:00 UTC, Adam D. Ruppe wrote:On Saturday, 12 January 2019 at 08:10:47 UTC, Andrey wrote:Yup, specifically (at least for DMD) it is defined here: https://github.com/dlang/druntime/blob/master/src/rt/alloca.dBut this requires using of C function "alloca". I think this cause some RT overhead (and in output asm code) in comparison with C++ variant. Or I'm not right?alloca is a magical function; a compiler intrinsic that works the same way as the C built-in.
Jan 12 2019