Preface: Did my research about struct alignment. Looked at this question, this one and also this one - but still did not find my answer.
My Actual Question:
Here is a code snippet I created in order to clarify my question:
#include "stdafx.h" #include <stdio.h> struct IntAndCharStruct { int a; char b; }; struct IntAndDoubleStruct { int a; double d; }; struct IntFloatAndDoubleStruct { int a; float c; double d; }; int main() { printf("Int: %d\n", sizeof(int)); printf("Float: %d\n", sizeof(float)); printf("Char: %d\n", sizeof(char)); printf("Double: %d\n", sizeof(double)); printf("IntAndCharStruct: %d\n", sizeof(IntAndCharStruct)); printf("IntAndDoubleStruct: %d\n", sizeof(IntAndDoubleStruct)); printf("IntFloatAndDoubleStruct: %d\n", sizeof(IntFloatAndDoubleStruct)); getchar(); } And it's output is:
Int: 4 Float: 4 Char: 1 Double: 8 IntAndCharStruct: 8 IntAndDoubleStruct: 16 IntFloatAndDoubleStruct: 16 I get the alignment seen in the IntAndCharStruct and in the IntAndDoubleStruct.
But I just don't get the IntFloatAndDoubleStruct one.
Simply put: Why isn't sizeof(IntFloatAndDoubleStruct) = 24?
Thanks in advance!
p.s: I'm using Visual-Studio 2017, standard console application.
Edit: Per comments, tested IntDoubleAndFloatStruct (different order of elements) and got 24 in the sizeof() - And I will be happy if answers will note and explain this case too.
intandfloatwithoutdoubleand why?sizeof()is"%zu".