On 13/12/2013, at 12:32 AM, srean wrote: > A rough idea of the problem: in C you're not allowed to index > past the end of an array. So strictly speaking this code is not > conforming C: > > struct X { char a[1]; }; > struct X *p = malloc(100); > char a = p->a[2]; // array is only 1 long! > > I have heard that this gets used in C a lot, what is the intended purpose of > such code ?
Dynamic allocation of a structure containing a variable length array at the end. Used in many thing, for example almost all serialisation formats, such as ACN 1.1 I guess. A common representation of strings (length bytes followed by the string). It is possible to do all this without such structures. It is possible to do all of C using pointers and casts. Its just harder to read. C89 doesn't have any support for this. You can have variable length arrays as function arguments, but not elsewhere (eg not in structures). -- john skaller skal...@users.sourceforge.net http://felix-lang.org ------------------------------------------------------------------------------ Rapidly troubleshoot problems before they affect your business. Most IT organizations don't have a clear picture of how application performance affects their revenue. With AppDynamics, you get 100% visibility into your Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro! http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk _______________________________________________ Felix-language mailing list Felix-language@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/felix-language