This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Alignment of structures.
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Re: Alignment of structures.
- From: Etienne LORRAIN <etienne_lorrain at yahoo dot fr>
- Date: Wed, 1 Dec 1999 12:43:22 +0100 (CET)
- Reply-To: etienne dot lorrain at ibm dot net
I think I can answer this one:
> If I add in an extra long into a struct definition the structures are not
> packed together, rather they are put on 32 byte boundary. This seems to
> be a bug in the Intel alignment. I tried to trim this down to a very
small > example.
Not a bug but a feature.
In the early days of microprocessor, the execution time of software
was reduced if you align address of each half word variable on a 2 bytes
boundary , and align address of each word variable on 4 bytes boundary.
(not speaking of microprocessor which cannot handle un-aligned access).
Now, processor are faster if you align big structures (>= 31 bytes)
on 32 bytes boundary - so the compiler will align your structures
sckw01 and sckw59 to start at an 32 byte boundary - that explain
the "zero filling" you get when "struct nkw" go from 32 bytes to 36
bytes. Your software should not depend on the fact that two contigous
variables would be contigous in memory.
If you need to, you can declare:
static struct {
NKW sckw0;
NKW sckw56;
NKW sckw65;
} sckw = { {...}, {...}, {...} };
And if you cannot touch the other source:
#define sckw0 sckw.sckw0
#define sckw56 sckw.sckw56
#define sckw65 sckw.sckw65
You can also have a look on the "packed" attribute of types,
in gcc documentation. You can also increase the alignment,
but unfortunately not deacrease it, by using the "align"
attribute, on the type or on the variable:
typedef struct nkw {...} __attribute__ ((align(64)));
or:
struct nkw sckw0 __attribute__ ((align(64)));
If you really need the previous system, you can recompile
GCC after completely removing in
gcc-2.95.*/gcc/config/i386/i386.h
the "#define":
CONSTANT_ALIGNMENT
DATA_ALIGNMENT
and maybe:
LOCAL_ALIGNMENT
The only real bad feature here is that you cannot reduce
alignment (which can be 32, 64, 128 or 256 bytes) when
you do not really care of the speed but more on the size
of the executable (-Os option) of for only one variable.
If you look deep in the documentation, you will find
the "packed" attribute for variables - but it does not work.
Hope that helps,
Etienne.
___________________________________________________________
Do You Yahoo!?
Achetez, vendez! À votre prix! Sur http://encheres.yahoo.fr