471,089 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

allocation alignment for global and local variables.

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
4 6929
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
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
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
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.

Similar topics

4 posts views Thread by Andrew V. Romero | last post: by
5 posts views Thread by Jim Ward | last post: by
15 posts views Thread by sethukr | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
112 posts views Thread by istillshine | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.