1

I was trying to find out the sizeof a structure, which I thought should show up as 24 bytes on my 64 bit Mac OS, instead it was shown as 32 bytes. what am i missing?

 #include<stdio.h>

 int main() {

 struct Test{
      int a;
      int *b;
      char *c;
      float d;
  }m;
  int size = sizeof(m);
  printf("%d\n",size);

}
Aparna Chaganti
  • 599
  • 2
  • 5
  • 15

2 Answers2

1

Any of the field is aligned to its minimal alignment which is 4 for ints and floats and 8 for pointers. There will be padding and memory unused before such alignment. The full structure is aligned to 16 due to SSE requirements:

a: offset 0
b: offset 8 (4 bytes padding before)
c: offset 16
d: offset 24
padding 4 bytes to align to 16 bytes.
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
0

Alignment. 4 bytes int, padding 4 bytes, 8 bytes pointer, eight bytes pointer, 4 bytes float, 4 bytes padding.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18