Does associative array change the location of values?

Paul Backus snarwin at
Fri Oct 29 17:58:24 UTC 2021

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.

More information about the Digitalmars-d-learn mailing list