Allocating a slice object

monarch_dodra monarchdodra at gmail.com
Thu Jul 4 05:02:13 PDT 2013


This is a pretty stupid question, but how would you allocate an 
"int[]" on the heap? I'm not talking about the array, but the 
actual slice object. EG:

int[]* pSlice = new int[];
//Error: new can only create structs,
//dynamic arrays or class objects, not int[]'s

Is there a simple "idiomatic" way?

I'm currently doing it by allocating a struct that wraps one:

struct S{int[] a;}
int[]* pSlice1 = cast(int[]*) new S;
int[]* pSlice2 = &(new S).a;

Note: This is also a neat way to allocate a static array on the 
heap.

Anybody have some better way?


More information about the Digitalmars-d-learn mailing list