opDispatch and compile time parameters
Timon Gehr via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 19 16:37:07 PDT 2015
On 10/19/2015 09:57 PM, Adam D. Ruppe wrote:
> 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).
This is the worst part:
class C{
int[] x=[1,2,3];
}
void main(){
auto mut=new C;
auto imm=new immutable(C);
assert(imm.x[0]==1);
mut.x[0]=2;
assert(imm.x[0]==2);
}
More information about the Digitalmars-d
mailing list