This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Structure alignment with gcc


youhaodeyi wrote:

> I found that the structure alignment of gcc is different between Linux and
> Windows. How can I change the default rule of the structure alignment? How
> can I make them consistent?

The Windows/MSVC ABI calls for doubles to be aligned to 64 bits. 
Linux/ELF only requires them aligned to 32 bits.  You can change this
with -malign-double/-mno-align-double, but ask yourself if you really
want to do that, as it involves breaking the ABI.  For example, if you
build Win32 code with -mno-align-double that interacts with any other
part of the system (and it would be quite difficult not to), you
potentially set yourself up for very strange crashes since your code
will not be following the correct ABI that everything else expects.  And
likewise the other way around by increasing the alignment of doubles on
Linux.  An ABI is something that everything on the system has to agree
on, otherwise all kinds of bad things happen.

Things also get even uglier if you have bitfields in your structs, or if
you use packed structs, because the two platforms specify different
layout rules there as well.  There are some tools you can use
(-mms-bitfields and __attribute((ms_struct/gcc_struct))__) but in
reality trying to get structs to always layout exactly the same across
platforms is just not a good idea.  If you have some binary file format
you need to read, you're much better off extracting each field
explicitly (e.g. with memcpy) than trying to expect a struct to have a
certain layout.

Brian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]