roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> In article <g1h08r$1c9f$1@pc-news.cogsci.ed.ac.uk>,
> Richard Tobin <richard@cogsci.ed.ac.uk> wrote:
>>In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
>>rahul <rahulsinner@gmail.com> wrote:
>
>>>How is the memory allocated for structures? I need to optimize the
>>>memory usage and bit fields are not doing the trick.
>
>>The members of a struct are stored in the order you specify.
>
> Expanding slightly on Richard's answer for emphasis:
>
> C *requires* that structure members be placed in the order given,
> and in increasing address order in memory. Storing the members in
> the order coded is not just a matter of convention: it is part
> of the language definition. As is the fact that alignment restrictions
> exist and are adhered to by the compiler by adding unnamed padding
> between structure members if needed (or advised) by the target machine.
Right, but it's perfectly legal for the alignment restriction to be
"any alignment is ok". It's also legal (but silly) for the compiler
to insert padding whether it's required for alignment or not.
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"