Does associative array change the location of values?
Steven Schveighoffer
schveiguy at gmail.com
Fri Oct 29 21:00:48 UTC 2021
On 10/29/21 1:58 PM, Paul Backus wrote:
> On Friday, 29 October 2021 at 17:40:38 UTC, Andrey Zherikov wrote:
>> I want to have a pointer to a value in an associative array. Does AA
>> guarantee that the value will remain at the same address all the time
>> unless I remove the corresponding key? I couldn't find any guarantees
>> similar to C++ iterator invalidation in D Language Reference.
>
> No, the AA does not guarantee that the value will remain in the same
> location. Inserting or removing *any* keys could cause the AA to resize,
> which may change the locations of all of its values.
>
> However, you do not have to worry about undefined behavior, because the
> garbage collector will keep the "stale" copy of the value alive as long
> as you hold a pointer to it.
This is incorrect, the buckets are each heap allocated. Just the array
of bucket pointers would change.
In addition, AAs do not deallocate the key/value pairs ever. You are
safe to obtain a pointer to a value and it will stay there, even if you
remove the key.
-Steve
More information about the Digitalmars-d-learn
mailing list