0

The size of below class as i think it should be 6 but the compiler show it's 8.

Anybody please explain why it is 8 instaed 6. Thanks

class Size{
    int a;
    char b;
    char c;
};
  • 1
    Padding bytes will be added to optimize memory access time. - https://fresh2refresh.com/c-programming/c-structure-padding/ – MayurK Feb 12 '20 at 11:11
  • fwiw nothing but common sense forbids the size of this struct to be eg 42. – 463035818_is_not_an_ai Feb 12 '20 at 11:13
  • It depends on what ISA (Instruction Set Architecture) you are developing on. I imagine you are using x86-64? If so, then I believe an int occupies 4 bytes, while characters occupy 1 byte. So as things stand, you are correct, the class would seemingly have 6 bytes total in size, but the compiler will automatically pad another 2 bytes to make the size of the class 8 bytes to optimize for cache transfers, among other things. – Mo Aboulmagd Feb 12 '20 at 11:16
  • https://en.cppreference.com/w/cpp/language/object#Alignment – Thomas Sablik Feb 12 '20 at 11:19
  • Thanks for clarifying. – arunreddy fulla Feb 12 '20 at 13:43

0 Answers0