here is a declaration of a C struct:
struct align
{
char c; //1 byte
short s;//2 bytes
};
On my environment, sizeof(struct align) is 4 and the padding 1 byte is between 'char c' and 'short s'. Some say that's because `short' has to be 2-byte aligned, so pading 1 byte is after 'char c'. On 32-bit machine, I know 'int' better be 4-byte aligned to prevent 2 memory read cycles since addresses sent on address bus between CPU and memory is a multiple of 4. But 'short' is 2 bytes, which is less than 4 bytes, so its address could be any byte within a 4-byte unit (except last byte).
multiple of 4 address -> |0|1|2|3|
I mean, 'short' can start at 0, 1, or 2. All can be retrieved by 1 read cycle, doesn't have to 0 or 2. In my 'struct align' case, 'char c' could be at 0, 'short s' could be at 1-2, padding could be at 3.
Why 2-byte long "short" has to be 2-byte aligned?
Thanks
Update my environment: gcc version 4.4.7, i686, Intel