Why does the sizeof operator when used on structures always gives different number of bytes than actually used by the structure to store the data(what i expect)?
Asked
Active
Viewed 163 times
1
-
1Padding. Field padding. – cnicutar Sep 02 '13 at 17:54
-
In what context? Because it does the `padding`. Read about structure padding. – Uchia Itachi Sep 02 '13 at 17:54
-
@cnicutar could you please elaborate on it? – chanzerre Sep 02 '13 at 17:55
-
have you read the K&R ? – Farouq Jouti Sep 02 '13 at 20:36
2 Answers
1
It doesn't always do that. It depends on the packing of the structure by the compiler. Paking depends upon the alignment rules of the architecture.

Charlie Burns
- 6,994
- 20
- 29
1
From THIS post
This is because of structure alignment. Structure alignment refers to the ability of the compiler to insert unused memory into a structure so that data members are optimally aligned for better performance. Many processors perform best when fundamental data types are stored at byte-addresses that are multiples of their sizes.
One can minimize the size of structures by putting the largest data types at the beginning of the structure and the smallest data types at the end of the structure