By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

allocation alignment for global and local variables.

P: n/a
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?

Thanks in advance.

Best regards
GAMJA

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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... There isn't a rule.
It's up to the implementation.
------------
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? No.

(well, noone prohibits you from expecting whatever you want ;-)
Does the C99 specification state any rules related with this problem?

No.
This is no problem.
Nov 15 '05 #2

P: n/a
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
Nov 15 '05 #3

P: n/a
Eric Sosman wrote:
[... memory order of global variables ...]
(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 ...)


Too bad you can't say "he got what he deserved", as it was you who ended
up getting it.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #4

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>,
"gamja" <as****@gmail.com> 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?


Every variable, whether static or local, is aligned in the way that it
needs to be aligned on your implementation. If your implementation
requires that the address of a struct foo or an int is aligned to a
multiple of four bytes, then it will be aligned that way.

On a different implementation, the alignment requirements might be
different.

You can't expect anything for the alignment of the variable "byte".
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.