I have declared only char
type members in the structure.
#include <stdio.h>
struct st
{
char c1;
char c2;
char c3;
char c4;
char c5;
};
int main() {
struct st s;
printf("%zu\n", sizeof(s));
return 0;
}
Output: [Live Demo]
5
So, why is there no padding in the structure for only char
type members?
Padding is to enforce alignment requeriments. A member is said to be aligned (to its size) if it is located at an address that is divisible by its size.
There is no need for padding in your example, since all members of the
st
structure are already aligned to their size, i.e.: the address of each member ofst
is already divisible by its size. All members are of typechar
and the size of achar
is 1. If a member's size is 1, that member is always aligned to its size, since any address is divisible by 1.The padding in structure exist (mostly) to enforce that the individual members are aligned to their fundamental alignment requirement, i.e. (C11 3.2p1):
The padding in the middle and at the end of the structure is used to ensure that even within an array of these structures, each member will be still aligned according to their alignment requirement. C11 6.2.8p1:
Now, the alignment requirement of every other type is implementation-defined, but one thing is implied: since the alignment requirement is expressed in
size_t
;sizeof (char)
is 1, and pointers to character types can be used to address each individual character in other types, a character type cannot have a fundamental alignment requirement of more than 1. Surprisingly this is not spelt out in the C standard at all; it just has this this vague wording (C11 6.2.8p6):As the alignment of
char
is at most 1, the compiler need not add any padding, because even if the structure is exactly 5 bytes long, then even in an array, with some structures starting at odd address, each of the members of those structures would still be properly aligned.Some types, when correctly aligned, have better performance/stability etc. When there are other variables behind a variable of such a type that makes its starting position "not well-aligned", the compiler may decide to add padding so the alignment goes well. This is all optional, and of course has no necessity when all you have are bytes (
char
).You will more likely observe padding if you writr a structure like this:
whose memory layout should look like this:
Then again, there's no need to align bytes since they are the mininal unit of storage.