Associative array on the heap
Freddy via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 18 17:31:48 PDT 2015
On Tuesday, 19 May 2015 at 00:00:30 UTC, Meta wrote:
> On Monday, 18 May 2015 at 23:55:40 UTC, 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."]
>
> They are allocated on the heap implicitly; there's no need for
> `new`. You actually *can't* use new with an AA, which is what
> the compiler is telling you.
>
> void main()
> {
> alias A = int[string];
> A b = []; //No allocation yet, b is null
> b["test"] = 1; //b is now non-null
> }
Sorry mis-phrased my question,
Who do you allocate a pointer to an associative
array(int[string]*).
More information about the Digitalmars-d-learn
mailing list