October 18, 2011 Possible bug with dynamic rectangular array initialization | ||||
---|---|---|---|---|
| ||||
Attachments: | 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 |
October 18, 2011 Re: Possible bug with dynamic rectangular array initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert | 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
|
Copyright © 1999-2021 by the D Language Foundation