1
#include<stdio.h>
int main()
{
    struct
    {
        char all[13];
        int foo;
    } record;

    printf("%d\n",sizeof(record.all));
    printf("%d\n",sizeof(record.foo));
    printf("%d\n",sizeof(record));
    return 0;
}

I have tried the above code and the output is:

13
4
20

Would you please explain this to me?

Harsha
  • 349
  • 1
  • 8
  • 20
  • 1
    btw, the specific answer for your case is that there are 3 padding bytes following `all` – Hasturkun Aug 15 '13 at 13:57
  • could u please explain me why the padding is exactly 3 bytes over here.... – Harsha Aug 15 '13 at 14:10
  • in this specific case, and assuming an x86, 32 bit system. the alignment requirement for an `int` is a multiple of 4. Since that's the case, the `int` can't be placed at offset 13 without padding. The extra padding needed is `(4 - (13 % 4)) == 3`, which rounds us up to 16, again a multiple of 4. – Hasturkun Aug 15 '13 at 14:23
  • -1: Search before posting please. – Lightness Races in Orbit Aug 15 '13 at 14:35

0 Answers0