Allocating a slice object
monarch_dodra
monarchdodra at gmail.com
Mon Jul 8 08:54:40 PDT 2013
On Monday, 8 July 2013 at 15:43:21 UTC, Marco Leise wrote:
> Am Thu, 04 Jul 2013 15:54:48 +0200
> schrieb "monarch_dodra" <monarchdodra at gmail.com>:
>
>> > This should work:
>> >
>> > int[] *pSlice = (new int[][1]).ptr;
>> >
>> > -Steve
>>
>> Hum... That would allocate a dynamic array of slices though
>> right? There'd be the Appendable overhead for just one
>> element...
>
> No, it allocates a static one-element array of int[]s and then
> returns the pointer to the first and only element. It's
> similar to wrapping it in a struct. So +1 for that solution.
I don't think that allocates a static array, it's just an
alternative syntax for dynamic array allocation. The fact that
you are extracting a pointer from it and it only has a single
element doesn't mean it is a static array.
//----
void main()
{
int[]* pSlice = (new int[][4]).ptr;
writeln(pSlice[0 .. 4].capacity);
}
//----
This prints 7 for me, which would simply be impossible for new
was allocating a static array.
More information about the Digitalmars-d-learn
mailing list