Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why short is 2-byte aligned?

Tags:

c

alignment

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

like image 994
password636 Avatar asked Apr 12 '14 06:04

password636


1 Answers

That is because a member of a struct is no difference from a single variable of that type, from the machine's perspective. Whatever alignment you choose, it applies to both.

For example, if short is two-byte long,

struct align
{
    char c;
    short s; // two-byte word
};

short ss; // two-byte word

The member s is of 2-byte type (e.g. WORD in IA32), exactly the same type of a "standalone" variable ss. The underlying architecture regards them as the same. So when there comes to an alignment requirement for that type, it just applies to both.

And if you add the padding at the end of the data, it may still be misaligned. Consider the start of ss is at the end of a 4-byte boundary.

like image 98
Eric Z Avatar answered Sep 30 '22 17:09

Eric Z