On Tue, May 14, 2013 at 9:41 AM, Olivier Langlois <olivier@xxxxxxxxxxxxxxxxxxx> wrote: > On Tue, 2013-05-14 at 08:52 +0200, Martti Kühne wrote: >> Sorry if this is OT and a dumb question, but are you sure you want >> >> On Mon, May 13, 2013 at 8:20 PM, LANGLOIS Olivier PIS -EXT >> <olivier.pis.langlois@xxxxxxxxxxxxxxxxxxxx> wrote: >> [...] >> > struct B >> > { >> > int numelem; >> > /* >> > * Old C trick to define a dynamically sizable array just by allocating >> > * sizeof(B) + (numelem-1)*sizeof(A) memory. >> > */ >> > A item[1]; >> > }; >> > >> >> one item vs. >> > That is a old C trick when STL container did not exist. The other > options would be to replace the 1 item array with pointer but then you > would have to do 2 malloc. 1 for struct B 1 for the array of A. > > Maybe my usage of B array did obfuscate the pattern. An another way to > use it is: > > B *p = (B *)malloc(sizeof(B) + (numelem-1)*sizeof(A)); > > That way you can define dynamically the say of the 'item' array. This > pattern dates back way before C99 standard which did, I think, introduce > dynamic array where you could write > > B array[var]; > > > C99 variable-length arrays are put on the stack, they're just a standard alloca. The standard/valid way of having a flexible-length struct member is `array[]` without a given size - no workaround is needed.