-
Size of Union
Hello,
In the following union, the size of the union variable displayed using sizeof operator is 52. If I comment the integer, the size is 50. If I include double variable also, the size is 56. What is the reason for this? The size should only be 50 right (size of Largest member)
Code:
union myUnion
{
int var1;
char char1;
//double mydouble1;
char string1[50];
};
-
My guess is alignment issues might be causing greater size; possibly because of a compiler bug or by design.
Tim S.
-
It's adding padding so that if you had an array of these unions, then each foo[i].var1 would be aligned to a 4 byte boundary (the size of an int here).
Or with a double, the padding ensures that each foo[i].mydouble1 would be aligned to a 8 byte boundary (the size of a double).