Re: gcc: loop do not terminate

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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];





[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]
  Powered by Linux