Does associative array change the location of values?
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Oct 29 18:19:29 UTC 2021
On Fri, Oct 29, 2021 at 05:58:24PM +0000, Paul Backus via Digitalmars-d-learn 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.
One way to achieve what the OP wants is to store a pointer to a
heap-allocated object in the AA. Then AA rehashing won't change the
value of the pointer.
Designer clothes: how to cover less by paying more.
More information about the Digitalmars-d-learn