Possible bug with dynamic rectangular array initialization

bearophile bearophileHUGS at lycos.com
Tue Oct 18 07:36:10 PDT 2011


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


More information about the Digitalmars-d-bugs mailing list