Associative array on the heap

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 7 12:29:32 UTC 2020


On 7/7/20 3:08 AM, mw wrote:
> On Tuesday, 19 May 2015 at 12:21:48 UTC, Steven Schveighoffer wrote:
>> 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
> 
> AA is a wrapper for a pointer (e.g a struct with some extra info beyond 
> the plain pointer), or AA is just the plain pointer (nothing extra)?

AA is a pImpl type wrapper. Yes, it's just a pointer inside.

But it's not simply a pointer because things like indexing can change 
the pointer (i.e. if the pointer is null, it will allocate a new AA impl).

If it were just a pointer, then using it without initializing would be a 
segfault.

-Steve


More information about the Digitalmars-d-learn mailing list