1

Consider the following code

#include <stdio.h>

typedef struct aStruct {
    int numA;
    int numB;
    int numC;
    int numD;
    int numE;
}A;

typedef struct bStruct {
    int numF;
    int numG;
    int numH;
    A* a;
}B;

int main(void) {
    printf("Size of struct A: %lu\n", sizeof (A));
    printf("Size of struct B: %lu\n", sizeof (B));

    return 0;
}

When I compile and run the code above on my 64-bit machine I get

Size of struct A: 20
Size of struct B: 24

I cannot understand why struct B comprises 24B. I would expect it to be 20B, 12B for the three integer members and 8B for the pointer. Initially I thought about memory alignment reasons, but according the the size of struct A, the system is able to allocate 20B.

Zois Tasoulas
  • 1,242
  • 1
  • 11
  • 23

0 Answers0