I have the following structure . Where the size is calculated on the side. The size of the structure should be 30Bytes after padding . But the size is 28 . How is the structure size 28?
#include <stdio.h>
struct a
{
char t; //1 byte+7 padding byte Total = 8bytes
double d; //8 bytes Total = 16bytes
short s; //2 bytes Total = 18Bytes
char arr[12];//12 bytes 8+8+4+12=32. Total = 30Bytes
};
int main(void)
{
printf("%d",sizeof(struct a)); // O/p = 28bytes
return 0;
}
You can use
offsetofto know the actual padding after each structure member:As
R.wrote, you are probably on a32-bitsystem where alignment ofdoubleis 4 bytes instead of 8.