www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Possible bug with dynamic rectangular array initialization

reply Robert <robert.welin gmail.com> writes:
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!A<F=S*0I[
M"B` ("!U:6YT('=I9'1H(#T -2P :&5I9VAT(#T -3L*("` (&)O;VQ;75M=
M(&=R:60["B` ("`*("` ("\O($%S<V5R="!T:&%T('1H92!L96YG=&  ;V8 
M=&AE(')O=R!I<R!W:61T:"!A;F0 =&AA="!A;&P =&AE(&5L96UE;G1S(&%R
M92!T<G5E"B` ("!V;VED(&-H96-K4F]W*&)O;VQ;72!R;W<I"B` ("!["B` 
M("` ("` 87-S97)T*')O=RYL96YG=&  /3T =VED=& I.PH ("` ("` (&9O
M<F5A8V  *&HL(&4[(')O=RD*("` ("` ("!["B` ("` ("` ("` (&%S<V5R
M="AR;W=;:ET /3T =')U92D["B` ("` ("` ?0H ("` ?0H ("` "B` ("`O
M+R!);FET:6%L:7IE('1H92!R96-T86YG;'5A<B!A<G)A>2!A;F0 <V5T(&%L
M;"!E;&5M96YT<R!T;R!T<G5E"B` ("!W<FET96QN*"));FET:6%L:7II;F< 
M<F5C=&%N9W5L87( 87)R87E<;B(I.PH ("` 9W)I9"YL96YG=&  /2!H96EG
M:'0["B` ("!A<W-E<G0H9W)I9"YL96YG=&  /3T :&5I9VAT*3L*("` (&9O
M<F5A8V  *&DL('([(&=R:60I"B` ("!["B` ("` ("` <BYL96YG=&  /2!W
M:61T:#L*("` ("` ("!R6UT /2!T<G5E.PH ("` ("` ('=R:71E;&XH(E1E
M<W1I;F< <F]W("(L(&DL("( :6X 9W)I9"(I.PH ("` ("` (&-H96-K4F]W
M*'(I.PH ("` ("` ('=R:71E;&XH(E)O=R`B+"!I+"`B('!A<W-E9"!T97-T
M+EQN(BD["B` ("!]"B` ("!W<FET96QN*")$;VYE+B!087-S960 =&5S=', 
M9'5R:6YG(&EN:71<;B(I.PH*("` ("\O($%S<V5R="!T:&%T(&%L;"!T:&4 
M<F]W<R!I;B!T:&4 9W)I9"!A<F4 =VAA="!T:&5Y('-H;W5L9"!B90H ("` 
M=W)I=&5L;B B5&5S=&EN9R!R96-T86YG=6QA<B!A<G)A>2!C;VYT96YT<R!A
M9V%I;EQN(BD["B` ("!A<W-E<G0H9W)I9"YL96YG=&  /3T :&5I9VAT*3L*
M("` (&9O<F5A8V  *&DL('([(&=R:60I"B` ("!["B` ("` ("` =W)I=&5L
M;B B5&5S=&EN9R!R;W< (BP :2P (B!I;B!G<FED(BD["B` ("` ("` 8VAE
M8VM2;W<H<BD["B` ("` ("` =W)I=&5L;B B4F]W("(L(&DL("( <&%S<V5D
M('1E<W0N7&XB*3L*("` ('T*("` ('=R:71E;&XH(E!A<W-E9"!T97-T<RY<
';B(I.PI]" ``
`
end
Oct 18 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Oct 18 2011