Memory allocation of structures

Joe Kreidler kreidler at cell.mot.COM
Tue Mar 6 01:06:40 AEST 1990


For the 64000, structures are word aligned. What I didn't
realize is that the end of the structure is also word aligned.
Let me explain what I mean by the previous sentence with an example.

Given the following declaration:

     struct {
             short a;
             short b;
             short c;
     } foo;
     short x;

For the compiler I'm using, "short" is an 8-bit integer. "foo" is
word aligned and uses three bytes. However, "x" is also word aligned.
Since "x" follows a structure it starts on a new word boundary.

This doesn't make sense to me. If the above declaration was changed to

     short a;
     short b;
     short c;
     short x;

then "x" would use the byte directly following "c". I was expecting the
first declaration to be allocated the same way.

Is there some reason for structures being allocated memory as I described
above? Is this observation part of the ANSI standard? Any insights are
appreciated.

Thanks in advance,
Joe

------------------------------------------------------------
Joe Kreidler, Motorola C.I.D     1501 W Shure Drive
...!uunet!motcid!kreidler        Arlington Heights, IL 60004
                                 708-632-4664
------------------------------------------------------------



More information about the Comp.lang.c mailing list