Jump to page: 1 2 3
Thread overview
Does associative array change the location of values?
Oct 29, 2021
Andrey Zherikov
Oct 29, 2021
Paul Backus
Oct 29, 2021
H. S. Teoh
Oct 29, 2021
Andrey Zherikov
Oct 30, 2021
Stanislav Blinov
Oct 30, 2021
Imperatorn
Oct 30, 2021
Andrey Zherikov
Oct 30, 2021
Stanislav Blinov
Oct 30, 2021
Imperatorn
Oct 30, 2021
Stanislav Blinov
Oct 30, 2021
Elronnd
Oct 30, 2021
Stanislav Blinov
Oct 31, 2021
Ali Çehreli
Oct 31, 2021
Elronnd
Oct 31, 2021
H. S. Teoh
Oct 31, 2021
Ali Çehreli
Oct 31, 2021
H. S. Teoh
Oct 31, 2021
Ali Çehreli
Oct 30, 2021
Stanislav Blinov
Oct 30, 2021
Stanislav Blinov
Oct 30, 2021
Stanislav Blinov
October 29, 2021

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.

October 29, 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.

October 29, 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.


T

-- 
Designer clothes: how to cover less by paying more.
October 29, 2021

On Friday, 29 October 2021 at 17:58:24 UTC, Paul Backus wrote:

>

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.

Thanks a lot for clarification

October 29, 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

October 30, 2021

On Friday, 29 October 2021 at 21:00:48 UTC, Steven Schveighoffer wrote:

>

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.

Who's going to document these implementation details? ;) I mean, if no one, then the above shouldn't be stated. Wouldn't you agree?

Given the premise of the question at hand, it does seem useful to know these. But at least one should stress what is and isn't subject to change (even if unlikely).

October 30, 2021

On Saturday, 30 October 2021 at 00:49:04 UTC, Stanislav Blinov wrote:

>

On Friday, 29 October 2021 at 21:00:48 UTC, Steven Schveighoffer wrote:

>

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.

Who's going to document these implementation details? ;) I mean, if no one, then the above shouldn't be stated. Wouldn't you agree?

Given the premise of the question at hand, it does seem useful to know these. But at least one should stress what is and isn't subject to change (even if unlikely).

This should be documented for sure

October 30, 2021

On 10/29/21 8:49 PM, Stanislav Blinov wrote:

>

On Friday, 29 October 2021 at 21:00:48 UTC, Steven Schveighoffer wrote:

>

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.

Who's going to document these implementation details? ;) I mean, if no one, then the above shouldn't be stated. Wouldn't you agree?

It should be documented. There isn't a valid way to remove these requirements, even if they are currently just an implementation detail -- code already depends on these properties. And D is a GC-based language, especially when using AAs. There is no reason to introduce undefined behavior for existing usage.

>

Given the premise of the question at hand, it does seem useful to know these. But at least one should stress what is and isn't subject to change (even if unlikely).

The whole AA documentation probably needs some attention.

-Steve

October 30, 2021

On Saturday, 30 October 2021 at 11:59:15 UTC, Steven Schveighoffer wrote:

>

It should be documented. There isn't a valid way to remove these requirements, even if they are currently just an implementation detail -- code already depends on these properties.

>

And D is a GC-based language, especially when using AAs. There is no reason to introduce undefined behavior for existing usage.

You won't introduce UB by deallocating unreferenced elements, which a given GC may be able to figure out. Therefore I object to "AAs do not deallocate the key/value pairs ever" part. Strongly :) Until such time that such a requirement is indeed set in stone, and not incidental.

October 30, 2021

On 10/30/21 10:51 AM, Stanislav Blinov wrote:

>

On Saturday, 30 October 2021 at 11:59:15 UTC, Steven Schveighoffer wrote:

>

It should be documented. There isn't a valid way to remove these requirements, even if they are currently just an implementation detail -- code already depends on these properties.

>

And D is a GC-based language, especially when using AAs. There is no reason to introduce undefined behavior for existing usage.

You won't introduce UB by deallocating unreferenced elements, which a given GC may be able to figure out. Therefore I object to "AAs do not deallocate the key/value pairs ever" part. Strongly :) Until such time that such a requirement is indeed set in stone, and not incidental.

auto v = k in aa;
aa.remove(k);

How can the GC/compiler work out that there is still a reference?

-Steve

« First   ‹ Prev
1 2 3