2
struct s1
{
    int a;
    char b; 
};

struct s2
{
    char b;
};

struct s3
{
    char a[3];
};

int main()
{
    s1 obj1;
    s2 obj2;
    s3 obj3;
    cout<<sizeof(obj1)<<endl; // prints 8
    cout<<sizeof(obj2)<<endl; // prints 1
    cout<<sizeof(obj3)<<endl; // prints 3
    return 0;
}

I have read the concept of structure padding, packing, and alignment of memory rules etc.... but I didn't understand the reason why there is no padding in case of struct s2 and s3 ?

EDIT: I am working on 32 bit-machine(word size is 4 bytes)

girish
  • 65
  • 6
  • can someone who marked it as duplicate please post the link where it was already answered. – girish Sep 02 '18 at 17:43
  • I believe any of the answers at the duplicate link applies well for your question, so I am not able to pick a particular one. And _someone_ was me BTW. – πάντα ῥεῖ Sep 02 '18 at 17:51
  • Ok. I have gone through all the 5 answers but none have discussed about the padding at the end of structures having only 1 member element in the structure. Can u say the reason in a sentence here. – girish Sep 02 '18 at 18:33

0 Answers0