[Issue 4475] Improving the compiler 'in' associative array can return just a bool

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Aug 15 10:49:05 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=4475



--- Comment #10 from bearophile_hugs at eml.cc 2013-08-15 10:49:03 PDT ---
(In reply to comment #9)

> Actually, that is not undefined. AA's are designed such that inserting new
> elements does not invalidate pointers to existing elements.

I didn't know this. Is this stated somewhere in the D specs?


> This holds even in the event of a rehash,

Associative arrays have to grow when you keep adding key-value pairs, I presume
this is done allocating a new larger hash (probably 2 or 4 times larger), and
copying data in it. In such situation aren't the pointers to the items becoming
invalid? Even if the doubling is done with a realloc, it can sometimes not be
able to reallocate in place.

To test my theory I have written a small test program:


void main() {
    enum size_t N = 1_000_000;
    bool[immutable uint] aa;
    auto pointers = new void*[N];

    foreach (immutable uint i; 0 .. N) {
        aa[i] = true;
        pointers[i] = i in aa;
    }

    foreach (immutable uint i; 0 .. N)
        assert(pointers[i] == (i in aa));
}


It gives no errors, so I am not understanding something. But are D specs
asserting this program will work in all D implementations?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list