Firstly, I understand byte padding in structures. But I still have a small test that contains a double field in the structure, and I donโt know how to explain it:
typedef struct { char a; double b; }data; typedef struct{ char a; int b; }single; int main(){ printf("%d\n",sizeof(double)); printf("%d\n",sizeof(single)); printf("%d\n",sizeof(data)); }
As a result of this test, the answer is: 8 8 and 16 .
Why does this result make me think?
In the second test, we can see that the word size on my machine is 4 bytes.
In the first test, we see that the size of double is 8 bytes.
So, in struct data : the result should be 12 bytes: 4 bytes for char and 8 bytes for double.
But I do not know why the result is 16 bytes. (So โโstrange to me)
Please explain it to me, thanks :)
source share