1

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)?

Jeremy
  • 22,188
  • 4
  • 68
  • 81
chanzerre
  • 2,409
  • 5
  • 20
  • 28

2 Answers2

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

Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33