AA invalidating pointers and references..?

simendsjo simen.endsjo at pandavre.com
Sun Aug 8 14:37:37 PDT 2010


On 08.08.2010 17:51, bearophile wrote:
> simendsjo:
>
>> 	auto a = [1:2];
>> 	auto p = 1 in a;
>> 	// can p be invalidated by rehashing?
>
> Yes, I presume it can. p is meant for immediate consumption only.
>
>
>> 	// The spec also says it orders in place, but returns the reorganized
>> array...
>> 	// Is the spec right? That it rehashes in place and returns a reference to
>> 	// itself?
>> 	assert(a is b);
>
> Yes, a and b are meant to be equal, because they are a reference, that doesn't change. What changes is the data structures referenced by it (if what I have just said turns out to be wrong, then probably it's an implementation bug that needs to be added to bugzilla).
>

Ok, thanks.

>
>> 	auto a = [1:2];
>> 	auto p = 1 in a;
>> 	a.remove(1);
>> 	// the memory for p can be reassigned by the gc,
>> 	// so this is undefined behavior.. right?
>
> Right, such things show that it's probably better to change the D AA design here:
> 1) make "x in AA" return a bool
> 2) improve dmd so it is able to remove most cases of dual lookups in AAs.
>
> I will think if this needs to become an enhancement request.

1)
I haven't worked much with AA's, but I find the "key in aa returns a 
reference to the value" to be handy. I think it's better than the following:

int value;
if( 1 in a )
   value = a[1];

or a[1] in a try/catch or other implementations.

2) I don't know what you mean. Does a single lookup often involve 
several under the hood?


More information about the Digitalmars-d-learn mailing list