allocation alignment for global and local variables.

G

gamja

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
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

gamja said:
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)
{
}
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.
 
E

Eric Sosman

gamja said:
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 ...)
 
K

Kenneth Brody

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:[email protected]>
 
C

Christian Bau

"gamja said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top