Let\'s consider the structs :
struct S1 {
int a;
char b;
};
struct S2 {
struct S1 s; /* struct needed to make this compile as C without ty
What is the reason behind the extra padding in structs?
If the processor is serious about alignment it raises an exception/signal , otherwise a performance penalty will be there as misalignment slowdown data access.
To understand this let's start with data structure alignment:
Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system) or larger. Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.
For example, when the computer's word size is 4 bytes (a byte means 8 bits on most machines, but could be different on some systems), the data to be read should be at a memory offset which is some multiple of 4. When this is not the case, e.g. the data starts at the
14th byte instead of the16th byte, then the computer has to read two 4 byte chunks and do some calculation before the requested data has been read, or it may generate an alignment fault. Even though the previous data structure ends at the 13th byte, the next data structure should start at the 16th byte. Two padding bytes are inserted between the two data structures to align the next data structure to the 16th byte.
When extending a padded struct, why can't extra fields be placed in the tail padding?
The compiler could place c in the padding in 6 7 8 without breaking alignment constraints. What is the rule that prevent it, and what is the reason behind it ?
Compiler could place it there but then the memory access to c will misalinged1 and there will be a performance penalty as explained above.To aling an array:
struct __attribute__((__packed__)) mypackedstruct{
char a;
int b;
char c;
};
This structure would have a compiled size of 6 bytes on a 32-bit system.
Unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.
1 A memory access is said to be aligned when the datum being accessed is n bytes long (where n is a power of 2) and the datum address is n-byte aligned. When a memory access is not aligned, it is said to be misaligned.