Associative array on the heap

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 19 05:21:48 PDT 2015


On 5/18/15 7:55 PM, Freddy wrote:
> How do you allocate an associative array on the heap?
> ----
> void main(){
>      alias A=int[string];
>      auto b=new A;
> }
> ----
> $ rdmd test
> test.d(4): Error: new can only create structs, dynamic arrays or class
> objects, not int[string]'s
> Failed: ["dmd", "-v", "-o-", "test.d", "-I."]

As others have said, I don't know why you would want to do this, since 
AA is already simply a wrapper for a pointer to a heap-allocated AA.

But if you wanted to, you could put it in a struct:

struct AA
{
    int[string] x;
}

void main()
{
    auto b = &((new AA).x);
}

-Steve


More information about the Digitalmars-d-learn mailing list