1
#include <stdio.h>

typedef struct {
    char a;
    short b;
    char c;
    int d;
} Test;

int main ()
{
    printf("%ld", sizeof(Test));
    return 0;
}

Why the ouput is 12 and not 8?

It seems that the compilers add the padding like this : char(1), short(2), padding(1), char(1), padding(3), int(4), so the result is 12, but why it doesn't put char, short, char, int so the result is 8?

  • Where do you get the numbers for your padding from? I would expect to get 1 byte padding before `b`, not after it. – Gerhardh Feb 07 '21 at 14:29
  • Typically (16 bit) short is aligned to 2-byte boundaries, and (32 bit) int to 4-byte boundaries. Usually because of hardware-efficiency reasons – Paul Hankin Feb 07 '21 at 14:29

0 Answers0