Hole of new? (Re: Array of array)

Timon Gehr timon.gehr at gmx.ch
Mon Jan 2 15:30:52 PST 2012

On 01/03/2012 12:02 AM, Mafi wrote:
> Am 02.01.2012 23:33, schrieb Timon Gehr:
>> On 01/02/2012 11:21 PM, RenatoL wrote:
>>> Just curious... the answer of the compiler it's a bit unclear to
>>> me...
>>> T[] is a dynamic array of type T.
>>> T[][] is a dynamic array of T[]. But this doesn't work. Why?
>> It does work. Why do you think it does not?
>> T[] a; // ok
>> T[][] b; // ok
>> auto c = new T[5]; // ok
>> auto d = new T[][5]; // ok
>> auto e = new T[]; // fail, nonsensical
>> auto f = new T[][]; // fail, nonsensical
> Here we come to an interesting point I often thought of. How do you
> allocate a T[] itself (so you get a T[]*) or a ClassType reference (so
> you get a ClassType*) on the heap (without casting)?
> As far as I know it's not possible with new.
> new T[n] is of type T[].
> new T[]* is of type T[]**.
> new ClassType is of type ClassType.
> new ClassType* is of type ClassType**.
> Is this a Hole of new?
> Mafi

Yes, but you can use (new T[][1]).ptr; to allocate T[] itself (and get a 
T[]*). Maybe new T[] should be interpreted as allocating a T[] and give 
back a T[]*.

More information about the Digitalmars-d-learn mailing list