digitalmars.D.learn - initializing a static array
- Simon =?UTF-8?B?QsO8cmdlcg==?= (17/17) Oct 10 2017 I have a static array inside a struct which I would like to be
- Andrea Fontana (3/8) Oct 10 2017 Maybe:
- jmh530 (4/6) Oct 10 2017 Alt:
- Andrea Fontana (2/4) Oct 10 2017 This works at runtime only for mutable arrays, anyway.
- Simon =?UTF-8?B?QsO8cmdlcg==?= (6/14) Oct 10 2017 This works fine, thanks a lot. I would have expected `.array` to
- Daniel Kozak (7/24) Oct 10 2017 On Tue, Oct 10, 2017 at 4:15 PM, Simon B=C3=BCrger via Digitalmars-d-lea...
- Adam D. Ruppe (6/8) Oct 10 2017 Not true here, the compiler knows it is going into a static array
- Daniel Kozak (3/12) Oct 10 2017 Yeah, you are right. My fault.
- Daniel Kozak (1/1) Oct 10 2017 https://run.dlang.io/is/SC3Fks
- kinke (7/17) Oct 10 2017 I hope this is not performance-critical code. The assembly is
- kinke (2/3) Oct 10 2017 Ah sorry, overlooked that it's the initializer for a struct field.
- Daniel Kozak (27/27) Oct 10 2017 struct Double
- Simon =?UTF-8?B?QsO8cmdlcg==?= (5/14) Oct 10 2017 Interesting approach. But this might introduce problems later.
- ag0aep6g (14/27) Oct 10 2017 Works for me:
I have a static array inside a struct which I would like to be initialized to all-zero like so struct Foo(size_t n) { double[n] bar = ... all zeroes ... } (note that the default-initializer of double is nan, and not zero) I tried double[n] bar = 0; // does not compile double[n] bar = {0}; // neither does this double[n] bar = [0]; // compiles, but only sets the first element, ignoring the rest Is there a good way to set them all to zero? The only way I can think of is using string-mixins to generate a string such as "[0,0,0,0]" with exactly n zeroes. But that seems quite an overkill for such a basic task. I suspect I might be missing something obvious here...
Oct 10 2017
On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote:Is there a good way to set them all to zero? The only way I can think of is using string-mixins to generate a string such as "[0,0,0,0]" with exactly n zeroes. But that seems quite an overkill for such a basic task. I suspect I might be missing something obvious here...Maybe: double[n] bar = 0.repeat(n).array;
Oct 10 2017
On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:Maybe: double[n] bar = 0.repeat(n).array;Alt: double[n] bar; bar[] = 0;
Oct 10 2017
On Tuesday, 10 October 2017 at 13:53:37 UTC, jmh530 wrote:double[n] bar; bar[] = 0;This works at runtime only for mutable arrays, anyway.
Oct 10 2017
On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote:This works fine, thanks a lot. I would have expected `.array` to return a dynamic array. But apparently the compiler is smart enough to know the length. Even the multi-dimensional case works fine: double[n][n] bar = 0.repeat(n).array.repeat(n).array;Is there a good way to set them all to zero? The only way I can think of is using string-mixins to generate a string such as "[0,0,0,0]" with exactly n zeroes. But that seems quite an overkill for such a basic task. I suspect I might be missing something obvious here...Maybe: double[n] bar = 0.repeat(n).array;
Oct 10 2017
On Tue, Oct 10, 2017 at 4:15 PM, Simon B=C3=BCrger via Digitalmars-d-learn = < digitalmars-d-learn puremagic.com> wrote:On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:fOn Tuesday, 10 October 2017 at 13:36:56 UTC, Simon B=C3=BCrger wrote:Is there a good way to set them all to zero? The only way I can think o=k. Iis using string-mixins to generate a string such as "[0,0,0,0]" with exactly n zeroes. But that seems quite an overkill for such a basic tas=It will return dynamic array. it is same as: double[5] =3D [0,0,0,0,0]; // this is still dynamicaly allocated.This works fine, thanks a lot. I would have expected `.array` to return a dynamic array. But apparently the compiler is smart enough to know the length. Even the multi-dimensional case works fine: double[n][n] bar =3D 0.repeat(n).array.repeat(n).array;suspect I might be missing something obvious here...Maybe: double[n] bar =3D 0.repeat(n).array;
Oct 10 2017
On Tuesday, 10 October 2017 at 14:42:15 UTC, Daniel Kozak wrote:It will return dynamic array. it is same as: double[5] = [0,0,0,0,0]; // this is still dynamicaly allocated.Not true here, the compiler knows it is going into a static array and puts the result directly in there. It handles literals. The range version though will allocate since the function doesn't know where its result ends up. It will CTFE though if the variable is static.
Oct 10 2017
Yeah, you are right. My fault. On Tue, Oct 10, 2017 at 4:47 PM, Adam D. Ruppe via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:On Tuesday, 10 October 2017 at 14:42:15 UTC, Daniel Kozak wrote:It will return dynamic array. it is same as: double[5] = [0,0,0,0,0]; // this is still dynamicaly allocated.Not true here, the compiler knows it is going into a static array and puts the result directly in there. It handles literals. The range version though will allocate since the function doesn't know where its result ends up. It will CTFE though if the variable is static.
Oct 10 2017
On Tuesday, 10 October 2017 at 14:15:07 UTC, Simon Bürger wrote:On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote:I hope this is not performance-critical code. The assembly is terrible for such code, at least for LDC, doing a GC allocation, unrolled reset to zero, then memcpying the dynamic array back to the stack: https://godbolt.org/g/uXBN75 `double[n] bar = void; bar[] = 0;` (2 lines, granted) results in a memset.Maybe: double[n] bar = 0.repeat(n).array;This works fine, thanks a lot. I would have expected `.array` to return a dynamic array. But apparently the compiler is smart enough to know the length. Even the multi-dimensional case works fine: double[n][n] bar = 0.repeat(n).array.repeat(n).array;
Oct 10 2017
On Tuesday, 10 October 2017 at 22:00:27 UTC, kinke wrote:[...]Ah sorry, overlooked that it's the initializer for a struct field.
Oct 10 2017
struct Double { double v =3D 0; alias v this; } struct Foo(size_t n) { Double[n] bar; } Dne 10. 10. 2017 3:40 odpoledne napsal u=C5=BEivatel "Simon B=C3=BCrger via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>: I have a static array inside a struct which I would like to be initialized to all-zero like so struct Foo(size_t n) { double[n] bar =3D ... all zeroes ... } (note that the default-initializer of double is nan, and not zero) I tried double[n] bar =3D 0; // does not compile double[n] bar =3D {0}; // neither does this double[n] bar =3D [0]; // compiles, but only sets the first element, ignoring the rest Is there a good way to set them all to zero? The only way I can think of is using string-mixins to generate a string such as "[0,0,0,0]" with exactly n zeroes. But that seems quite an overkill for such a basic task. I suspect I might be missing something obvious here...
Oct 10 2017
On Tuesday, 10 October 2017 at 13:54:16 UTC, Daniel Kozak wrote:struct Double { double v = 0; alias v this; } struct Foo(size_t n) { Double[n] bar; }Interesting approach. But this might introduce problems later. For example `Double` is implicitly convertible to `double`, but `Double[]` is not implicitly convertible to `double[]`. Therefore I will stick with jmh530's solution for now, but thank you anyway.
Oct 10 2017
On 10/10/2017 03:36 PM, Simon Bürger wrote:I have a static array inside a struct which I would like to be initialized to all-zero like so struct Foo(size_t n) { double[n] bar = ... all zeroes ... } (note that the default-initializer of double is nan, and not zero) I tried double[n] bar = 0; // does not compileWorks for me: ---- struct Foo(size_t n) { double[n] bar = 0; } void main() { import std.stdio; Foo!5 foo; writeln(foo.bar); /* prints "[0, 0, 0, 0, 0]" */ } ----
Oct 10 2017