Re: Structure alignment with gcc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux