Stupid question about AA. The following code works but I don't undrstand why?!

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 9 09:44:06 PDT 2016


On 09.04.2016 18:13, Uranuz wrote:
> http://dpaste.dzfl.pl/523781df67ab

For reference, the code:
----
import std.stdio;

void main()
{
	string[][string] mapka;
	
	string[]* mapElem = "item" in mapka; //Checking if I have item
	
	if( !mapElem )
		mapElem = &( mapka["item"] = [] ); //Creating empty element inside map
	
	writeln( (*mapElem).capacity );
	
	//Appending should reallocate, so pointer to array should change
	*mapElem ~= ["dog", "cat", "horse", "penguin", "fish", "frog"];

	//But AA still somehow knows the right pointer
	writeln(mapka);
	
	//It works, but I dont understand why?
}
----

mapElem is not a pointer to the elements of the array. It's a pointer to 
the dynamic array structure which holds the pointer to the data and the 
length. That means, the reallocation doesn't change mapElem. It changes 
(*mapElem).ptr.


More information about the Digitalmars-d-learn mailing list