www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - allocate an array of AA?

reply sa <no where.com> writes:
How to allocate an array of AA?

I tried two ways (new, and setting array length), in both cases the code
compiles, but the executable seems run into infinite loop, and never finish.

void allocAA() {
  int[int][] maps;
  maps = new int[int][3];
  maps.length = 3;
}

int main() {
  allocAA();

  return 0;
}
Mar 08 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
sa Wrote:
 How to allocate an array of AA?
 I tried two ways (new, and setting array length), in both cases the code
 compiles, but the executable seems run into infinite loop, and never finish.
It's a bug of DMD. I think you have to use a loop with a creation followed by an append (~=) to the array. Bye, bearophile
Mar 08 2008
parent reply sa <no where.com> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 sa Wrote:
 How to allocate an array of AA?
 I tried two ways (new, and setting array length), in both cases the code
 compiles, but the executable seems run into infinite loop, and never finish.
It's a bug of DMD. I think you have to use a loop with a creation followed by an
append (~=) to the array. But how to write such 'a creation'? i.e. how to new an AA? what's the syntax? tried: int[int]* mapsp; mapsp = new int[int]; // this line The compiler complains: need size of rightmost array, not type int
Mar 08 2008
parent bearophile <bearophileHUGS lycos.com> writes:
sa:
 But how to write such 'a creation'? i.e. how to new an AA? what's the syntax?
I think some sugar can be added to the language for this. In the meantime you can use this from my D libs: /*************************************** Return a new empty associative array of the given KeyType,ValueType. Example: AA!(int, string); */ template AA(KeyType, ValueType) { const ValueType[KeyType] AA = AA_impl!(KeyType, ValueType).res; } private template AA_impl(KeyType, ValueType) { ValueType[KeyType] result; const ValueType[KeyType] res = result; } to be used as something as: aaa char[int][]; foreach(_; xrange(10)) aaa ~= AA!(int, char); Bye, bearophile
Mar 08 2008