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

Re: [MiNT] struct align



Hi,

On Sunday 11 January 2009, Miro Kropacek wrote:
> You see -- the 3rd member is put xxxxxC address (i.e. divisible by 4),
> fine. But now, on both atari compilers is then 'long' member aligned on 2
> bytes (xxxxE, which is 14 for x = 0, i.e. divisible only by 2) what I
> find wrong since it should work according to the data type (char =
> aligned on 1 byte boundary, short = 2 bytes, long = 4 bytes, ...)

No, the alignment is decided based on what the CPU requires and which
is faster.

> and 
> this assumption supports the fact it works in that way on linux's gcc.
> But I have no idea, what's wrong here? linker setup or gcc setup?

If I've understood correctly:
- On 68k, 2-divisable address is OK for both shorts & longs
- On x86 you don't need alignment, but natural alignment is faster.

The structure alignment depends on compiler and you can override it[1]
if you know what you're doing. C standard guarantees only that the structure
members are in the specified order.  I.e. to save space, you need to
manually make sure that your structure members are of optimal type and
order.  Because of caches, proper order of struct members is often important
for performance too (if most often used struct members fit the same
cacheline or not).

[1] http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html


	- Eero