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;
ziad
  • 11
  • 1
  • [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/q/119123/995714) – phuclv Sep 14 '20 at 02:56

1 Answers1

0

On your architecture all objects of type long int must be at addresses that are 4-byte aligned, and short ints 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.