[Issue 7564] Implicit conversion from static to dynamic array in loops

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 22 16:04:19 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7564


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #1 from bearophile_hugs at eml.cc 2012-02-22 16:04:17 PST ---
I think there is no bug here.

This line:
a[i] = b;

means
a[i] = b[];

That means:
a[i].ptr = b.ptr;
a[i].length = b.length;

So every time you are rebinding a[i] to the same memory location.

See the output of this program:


import std.stdio, std.algorithm;

void foo(int[][] a) {
    foreach (i; 0 .. 3) {
      int[1] b = i;
      a[i] = b[];
      writeln(a, " ", map!(row => row.ptr)(a));
    }
}

void main() {
    int[][] a;
    a.length = 3;

    foo(a);
    writeln(a);
}


Your output is generated by:

import std.stdio, std.algorithm;
void main() {
    int[][] a;
    a.length = 3;

    foreach (i; 0 .. 3) {
      int[1] b = i;
      a[i] = b[].dup;
      writeln(a);
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list