AA invalidating pointers and references..?

bearophile bearophileHUGS at lycos.com
Sun Aug 8 08:51:45 PDT 2010


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).


> 	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.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list