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?
The padding in structure exist (mostly) to enforce that the individual members are aligned to their fundamental alignment requirement, i.e. (C11 3.2p1):
requirement that objects of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address
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:
Complete object types have alignment requirements which place restrictions on the addresses at which objects of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. An object type imposes an alignment requirement on every object of that type: stricter alignment can be requested using the _Alignas keyword.
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):
The types char
, signed char
, and unsigned char
shall have the weakest alignment requirement.
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.
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 of st
is already divisible by its size. All members are of type char
and the size of a char
is 1. If a member's size is 1, that member is always aligned to its size, since any address is divisible by 1.
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:
struct pad{
int8_t a;
int64_t b;
};
assert(sizeof(struct pad) == 16);
whose memory layout should look like this:
|---|-------|---------|
| 1 | 7 | 8 |
|---|-------|---------|
byte|padding|64-bit int
Then again, there's no need to align bytes since they are the mininal unit of storage.