0

How its union size 12 ?

#include <stdio.h> #include <string.h> int main() { union check { int num; char name[10]; }test; printf ("%ld",sizeof (test)); return 0; }

15th Jun 2020, 8:26 AM
RISWAN MOHAMED
RISWAN MOHAMED - avatar
3 Respostas
+ 3
Size of a union is the size of its biggest member, a member that requires bytes most. Plus some padding bytes to round the allocated memory size up to the nearest multiple of 4 bytes. In this case the biggest member is <name> which requires 10 bytes, rounded up to 12, the next multiple of 4. Hth, cmiiw
15th Jun 2020, 9:24 AM
Ipang
+ 1
I believe it has something to do with an integer filling up the union's size to the nearest multiple of 4. Every character takes up a single byte and the integer fills up to the nearest multiple. In your case, the int fills up to 12. Try changing the amount of characters in the array.
15th Jun 2020, 8:37 AM
Thijmen
Thijmen - avatar
+ 1
Thanks brothers i understood..
15th Jun 2020, 1:19 PM
RISWAN MOHAMED
RISWAN MOHAMED - avatar