www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array on the heap

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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."]
May 18 2015
next sibling parent reply "Meta" <jared771 gmail.com> writes:
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 }
May 18 2015
next sibling parent "Meta" <jared771 gmail.com> writes:
On Tuesday, 19 May 2015 at 00:00:30 UTC, Meta wrote:
     A b = []; //No allocation yet, b is null
Whoops, you actually can't assign the empty array literal to an AA. This line should be: A b; Which has the exact same effects.
May 18 2015
prev sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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]*).
May 18 2015
parent "Xinok" <xinok live.com> writes:
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;
May 18 2015
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 18 May 2015 23:55:38 +0000, Freddy wrote:

 How do you allocate an associative array on the heap?
 ----
 void main(){
 	alias A=3Dint[string];
 	auto b=3Dnew 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."]
AAs are always allocated on heap, you don't need to do anything special.=
May 18 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
May 19 2015