Thread overview
[Issue 7564] New: Implicit conversion from static to dynamic array in loops
Feb 22, 2012
Sebastian
Feb 23, 2012
Sebastian
Feb 23, 2012
Kenji Hara
February 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7564

           Summary: Implicit conversion from static to dynamic array in
                    loops
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: sebastian.sandberg1000@gmail.com


--- Comment #0 from Sebastian <sebastian.sandberg1000@gmail.com> 2012-02-22 15:24:43 PST ---
import std.stdio;

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

    for(int i; i<3; i++) {
      int[1] b;
      b[0] = i;
      a[i] = b;
      writeln(a);
    }
}

Result:
[[0], [], []]
[[1], [1], []]
[[2], [2], [2]]

I expected the result:
[[0], [], []]
[[0], [1], []]
[[0], [1], [2]]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7564


bearophile_hugs@eml.cc changed:

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


--- Comment #1 from bearophile_hugs@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: -------
February 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7564



--- Comment #2 from Sebastian <sebastian.sandberg1000@gmail.com> 2012-02-23 13:40:11 PST ---
Ok, I understand, I was wrong. However on a related matter, I think that,
import std.stdio;
int[1] f(int i)
{
  int[1] a = i;
  return a;
}
void main() {
   foreach (i; 0 .. 2) {
     writeln(f(i).ptr);
   }
  writeln(f(0).ptr);
  writeln(f(1).ptr);
}

prints
7FFFF03A05E0
7FFFF03A05E0
7FFFF03A05E4
7FFFF03A05E8

is a little bit strange, but I guess that just how it works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7564



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-02-23 14:48:52 PST ---
Temporary objects are destroyed ad the scope end, not statement end.

(In reply to comment #2)
> Ok, I understand, I was wrong. However on a related matter, I think that,
> import std.stdio;
> int[1] f(int i)
> {
>   int[1] a = i;
>   return a;
> }
> void main() {
>    foreach (i; 0 .. 2) {
>      writeln(f(i).ptr);

is same as:
       auto tmp = f(i); writefln(tmp.ptr);
       // tmp is destroyed at the end of foreach, so prints same address

>    }
>   writeln(f(0).ptr);
>   writeln(f(1).ptr);

are same as:
       auto tmp1 = f(i); writefln(tmp1.ptr);
       auto tmp2 = f(i); writefln(tmp2.ptr);
       // each temporaries has own memory, so prints different address

> }
> 
> prints
> 7FFFF03A05E0
> 7FFFF03A05E0
> 7FFFF03A05E4
> 7FFFF03A05E8
> 
> is a little bit strange, but I guess that just how it works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7564


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #4 from bearophile_hugs@eml.cc 2012-02-23 16:58:52 PST ---
I close this, there is a bug, but it's reported elsewhere in bugzilla ( Issue
7444 ).

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