Re: Possible bug with dynamic rectangular array initialization

2011-10-18 Thread bearophile
Robert:

> When I try to initialize a dynamic rectangular array, the length of the inner
> arrays does not seem to be preserved outside the scope of initialization. I'm
> not proficient enough in D to say that my code is completely correct, but if
> I am correct this might be a serious bug. See attached test code.

Even when you think you have found a bug in D, I suggest you to ask first in 
D.learn newsgroup. D.bugs is not meant for free discussions.

Regarding your code, you have hit a bug-prone spot of D, but I think D is 
correct here.

This code of yours:

foreach (i, r; grid)
{
r.length = width;

Write it like:

foreach (i, ref r; grid)
{
r.length = width;

Because D arrays are value types that contain a pointer to the storage. So the 
r inside the foreach is a copy, you aren't modifying the original. See the D 
ABI to see how D dynamic arrays are implemented on their surface (it doesn't 
explain how array append is managed at runtime).

Bye,
bearophile


Possible bug with dynamic rectangular array initialization

2011-10-18 Thread Robert
Hello,
When I try to initialize a dynamic rectangular array, the length of the inner
arrays does not seem to be preserved outside the scope of initialization. I'm
not proficient enough in D to say that my code is completely correct, but if
I am correct this might be a serious bug. See attached test code.
Many thanks
Robert
begin 644 arrayinit.d
M:6UP;W)T('-T9"YS=&1I;SL*"G9O:60@;6%I;BAS=')I;F=;72!A2!A;F0@2!C;VYT96YT