How to create a fixed structure header for a variable size array

I want to create a fixed size structure header for a variable size array in the D programming language .. In "C" it would be possible to place a zero length or empty Array as the last element declared within the fixed structure header and then set up calls to Malloc to add the extra storage required by part of the data structure, the size of which depends on the size that the first element will reference this last declaration.

But in D language, Array is a more advanced object, and when I try to create a set of Opcode structured strings, I really want to express a composite structure with a reference to the target memory as its last element (the first element of the array following it.

How do I declare / create / work with the structure of a variable length variable length struct when using D programming language?

+3


source to share


1 answer


this is exactly the same as in c

struct VarArray(T){
uint length;
T[0] t;

static VarArray!T* allocate(T)(uint length){
VarArray!T* ret = enforce(malloc((VarArray!T).sizeof+T.sizeof*length));
*ret.length=length;
return ret;
}

}

      



check out http://dlang.org/arrays.html#static-arrays :

A static array of size 0 is allowed, but there is no space allocated for it. It is useful as the last element of a variable length struct, or as a degenerate case of template expansion.

+6


source







All Articles