Associative array on the heap

Xinok via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 17:44:13 PDT 2015


On Tuesday, 19 May 2015 at 00:31:50 UTC, Freddy wrote:
> Sorry mis-phrased my question,
>  Who do you allocate a pointer to an associative 
> array(int[string]*).

Ignoring the why for a moment, one trick is to place it in an 
array literal so it's heap allocated. This requires writing an 
associative array literal with a single key-element pair though.

int[string]* a = [["zero":0]].ptr;


Another trick is to initially define the associative array in a 
class. Since classes are heap allocated, you can allocate an 
instance of the class and grab a pointer to the associative array.

class HeapAA
{
     int[string] a;
}

int[string]*b = &(new HeapAA).a;


More information about the Digitalmars-d-learn mailing list