According to the C++ standard:
The size of a union is sufficient to contain the
largest of its data members. Each data member is allocated as if it
were the sole member of a struct.
So the memory layout of the union is the same as if you had a long
there, an array of 5 int
s there, or a char
there, but spaced to the largest of these (the array of int
s). I presume you're using GCC, and IIRC GCC sets a size of 32-bits for int
even on a 64-bit architecture. UDATE would therefore have a size of 20-bytes. You'd naively expect, therefore, sizeof(struct data) + sizeof(temp)
to return 52. The fact that you're apparently getting 64 is probably because GCC is aligning things to 64-bit boundaries and thus taking 24-bytes for UDATE, and inserting a 4-byte spacer between cat
and cow
in your data
struct.
Note that the standard makes no guarantees made about type-punning - that is the frequently used low level technique where the union is written using one method and read using another to, for example, access the bytes that make up an integer - and claims that a union may only be accessed using the same entries that it was written using (baring an exception for a union of POD structs that share common initial members). However, in practice every compiler I've ever used will read the memory in the manner you'd expect if you'd simply cast a pointer from one type to another in the union using a reinterpret or C-style cast.