www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7564] New: Implicit conversion from static to dynamic array in loops

reply d-bugmail puremagic.com writes:
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



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: -------
Feb 22 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7564


bearophile_hugs eml.cc changed:

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



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: -------
Feb 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7564




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: -------
Feb 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7564




Temporary objects are destroyed ad the scope end, not statement end.


 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: -------
Feb 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7564


bearophile_hugs eml.cc changed:

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



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: -------
Feb 23 2012