4

I understand how padding works. I know what the alignment is. What is strange for me is, why the size of the struct with char fields only is not aligned to 4 bytes (padding at the end)? I suspect this is simply not guaranteed by the specification, so compiler does not do that. If that's the case, can I get the reference to such rule? I'm mostly interested in x86 and x86-64 architectures.

Example:

struct foo {
    char field1;
    char field2;
    char field3;
} foo2;

int main(void)
{
    printf("sizeof=%lu\n", sizeof foo2);
}

output: sizeof=3

Chnossos
  • 9,971
  • 4
  • 28
  • 40
hencew5
  • 43
  • 2
  • 1
    Where is your padding? You have 3 `char`s, therefore you have 3 bytes. – drum Jul 21 '17 at 04:29
  • 4
    The structure doesn't need to be padded at the end, as bytes (`char` is an 8-bit byte on most platforms) on most systems can be accessed on both odd and even addresses, i.e. their alignment is `1`. – Some programmer dude Jul 21 '17 at 04:30
  • 1
    In the ARM OABI (apcs-gnu) this would be 4-byte aligned. (Don't ask me why, that ABI doesn't make a lot of sense) – M.M Jul 21 '17 at 06:05
  • @M.M that's quite interesting. Is there any document with this spec available? I was able to find only some newer ones. – hencew5 Jul 21 '17 at 08:01
  • @hencew5 [as far as I have been able to determine](https://stackoverflow.com/questions/43786747/struct-layout-in-apcs-gnu-abi), there's no spec. Instead , the ABI is just "what gcc did". The new spec (EABI) was sorely needed. – M.M Jul 21 '17 at 09:30

1 Answers1

5

The alignment of a structure must be such that all its fields are also aligned correctly (especially if they're part of an array).

Since all the fields here have an alignment requirement of one, that's also the alignment requirement of the structure itself.

In other words, if you put two of these structures next to each other, no field in either structure would violate the alignment requirement.

Contrast that with:

struct moreComplexCase {
    char char1WithAlignment1;
    // 3 byte gap to align below item.
    unint32_t uintWithAlignment4;
    char char2WithAlignment1;
    // 3 byte gap to align structure itself.
}

You'll see the two padding sections there. The first is to ensure the uint32_t is properly aligned (you should also be able to see that the structure alignment needs to be four as well so that the uint32_t aligns correctly with the given padding).

The final padding is to ensure a second element of an array (and, for that matter, all subsequent elements) will also be aligned correctly.

The C standard itself doesn't dictate what the alignment is, just that alignment may exist. For example, early x86 chips (and possibly the current ones) handle misaligned data just fine (or may do it a little slower) while other architectures (e.g., early ARM chips) will simply crash or raise a fault if you try to do that.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Ok, I think I've got it. The problem was I thought that it is better to access to this struct on the addresses which are dividable by the native size of the memory address (4 bytes on x86 and 8 bytes on x86-64). However, do you know if this is true for any other architectures, like ARM or PowerPC? Is it fully portable? – hencew5 Jul 21 '17 at 04:48
  • Quite a lot of CPUs support loading of individual bytes from any address. What is mainly happening is that an entire cache line is being filled so it's relatively easy from then on to get the byte into a register. Things get interesting on machines that don't, like Prime mainframes (ancient history) . They had 16 bit words, which was the smallest unit of data that could be loaded into a register. – bazza Jul 21 '17 at 05:32
  • "while other architectures will fold like a cheap suit if you try to do that". Does it mean that unaligned access doesn't require much or any overhead on such architectures? I'm not a native speaker, so I'm quite confused what does the "fold" mean in this context:) – hencew5 Jul 21 '17 at 05:35
  • Sorry, @hencew5, will make it a little less regional :-) "Fold like a cheap suit" means it will collapse badly. – paxdiablo Jul 21 '17 at 05:38