opDispatch and compile time parameters

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 19 12:57:02 PDT 2015


On Monday, 19 October 2015 at 19:53:14 UTC, Andrei Alexandrescu 
wrote:
> struct A {
>     int[] x = new int[10];
> }
>
> void main() {
>     import std.stdio;
>     A a;
>     a.x[1] = 42;
>     writeln(a.x);
> }
>
> Looks like a bona fide runtime array to me.

It is still in the static data segment. Try this:


struct A {
     int[] x = new int[10];
}

void main() {
     import std.stdio;
     A a;
     a.x[1] = 42;
     writeln(a.x);

     A a2;
     writeln(a2.x);

     assert(a.ptr is a2.ptr); // passes
}


The `new int[10]` is done at compile time and the pointer it 
produces is put into the .init for the struct. So the same 
pointer gets blitted over to ALL instances of `A`, meaning they 
alias the same data (until the slice gets reallocated by append 
or whatever).


More information about the Digitalmars-d mailing list