Dynamically Sized Structs

monarch_dodra monarchdodra at gmail.com
Wed Apr 16 22:57:14 PDT 2014


On Thursday, 17 April 2014 at 00:55:19 UTC, Dicebot wrote:
> Just in case, the key line to pay attention to in that example 
> is this one:
>
> CellIndex[0] c_;
>
> It is a commonly used C idiom for dynamically sized structures 
> that D also supports.

Absolutely. However, from a technical point of view, it doesn't 
make the structure "dynamically sized". It is only a hack to 
access data past the end of the struct.

You still have to manually and dynamically allocate the struct:
auto p = cast(State*)malloc(blockSize * stateSize);

And accessing data is done via .ptr, to avoid out of bounds.

     CellIndex get(in size_t i) inout pure nothrow {
         return c_.ptr[i];
     }


More information about the Digitalmars-d-learn mailing list