gamja wrote:
Hi all.
I know that some padding bits are inserted between data members of a
structure. Is this rule also applied for the variables on local stack
or global??? For example, in following code dumps...
------------
struct foo foo1;
...
struct bar bar1;
...
char byte;
int dword;
int main(void)
{
}
--------------
Is it allowed to expect that each start address of foo1, bar1, byte and
dword are aligned 4bytes?
Does the C99 specification state any rules related with this problem?
Each of foo1, bar1, byte, and dword will meet its type's
alignment requirements, whatever those are. There are no
rules about where in memory the four variables reside, nor
about their order. Since the order is unspecified, questions
of padding "between" the variables aren't meaningful.
(I once had a lot of trouble porting a large program from
one machine to the other. On the original machine, file-scope
variables within a module were allocated to ascending memory
addresses in the order they were declared -- so foo1 would be
followed by bar1 in the example above -- and the variables from
different modules were allocated in the order in which the
modules were linked. On the target machine I was porting to,
though, all globals from all modules were pooled together and
sorted alphabetically by their names. It turned out that the
program's author relied on the allocate-as-declared order ...)
--
Eric Sosman
es*****@acm-dot-org.invalid