Can pointers to values inside associative arrays become invalid?
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 6 09:43:52 PST 2015
On Tue, Jan 06, 2015 at 05:39:50PM +0000, Idan Arye via Digitalmars-d-learn wrote:
[...]
> I see... quite a shame there are no built-in data structures that
> provide forever-valid references to their members. Arrays can be
> reallocated, associative arrays can be rehased, and the stuff in
> std.collection does not expose the internal references...
Internal references are not exposed precisely because user code
shouldn't depend on it.
If you want forever-valid references to container members, just store
references to your data instead of the data itself, e.g., MyStruct*[]
(as long as you make sure your structs are allocated on the heap). Or
use classes, which are reference types, e.g., elements of MyClass[] will
be "forever valid".
T
--
The richest man is not he who has the most, but he who needs the least.
More information about the Digitalmars-d-learn
mailing list