Why the size of this structure is 16 on a 32-bit architecture?
struct data {
char x;
long int y;
char z;
short int s;
char l;
} data_1;
Why the size of this structure is 16 on a 32-bit architecture?
struct data {
char x;
long int y;
char z;
short int s;
char l;
} data_1;
On your architecture all objects of type long int
must be at addresses that are 4-byte aligned, and short int
s 2-aligned. Since the members must be laid out in memory in that order, there will be 3-byte padding after x
; 1-byte padding after z
, and 3 bytes after l
. The members themselves require 9 bytes for char is 1 byte, short 2 and long 4; and 7 bytes were wasted for padding.
The size will drop to 12 bytes by simply placing char x;
after long int y;
. Then there will be only 3 bytes of padding, after l
.