Here is the simple example:
All the members in Union will share the same memory location.
We have a same set of members for MyStruct and MyUnion. Compiler will
allocate 16 bytes for the given structure and allocate just 4 bytes
for the union.
struct MyStruct
{
long lValue;
char ch;
char buf[4];
float f;
};
union MyUnion
{
long lValue;
char ch;
char buf[4];
float f;
};
int _tmain(int argc, _TCHAR* argv[])
{
int sz1 = sizeof(MyStruct); // will be 16 bytes
int sz2 = sizeof(MyUnion); // will be 4 bytes
}
An example in VC++ would be VARIANT class.
To view more details on sizeof, visit at
http://www.kathircpp.com