I know the differences between union and structure. But from a design and coding perspective what are the various use cases of using a union instead of a structure? One is a space optimization. Are there any more advantages of using them?
There's really only two major uses. The first is to create a discriminated union. That's probably what you were thinking of by "space optimization," but there's a bit more to it. You need an extra bit of data to know which member of the union is "alive" (has valid data in it) since the compiler does not do this for you. You'd usually see code like that having a union inside of a struct, something like:
struct mixed {
enum { TYPE_INT, TYPE_FLOAT } type;
union {
int int_value;
float float_value;
} data;
};
When assigning to either data.int_value or data.float_value, you'd be expected to set the type member to the appropriate enumeration value as well. Then your code using the mixed value can figure out whether to read the int_value or float_value.
The second significant use of unions is to offer an API that allows the user to access the same data multiple ways. This is pretty limited, as it requires all types in the union to be laid in memory in identical ways. For example, an int and a float are entirely different, and accessing a float as an integer does not give you any particularly meaningful data.
For a useful example of this use case, see how many networking APIs will define a union for IPv4 addresses, something like:
union ipv4addr {
unsigned address;
char octets[4];
};
Most code just wants to pass around the 32-bit integer value, but some code wants to read the individual octets (bytes). This is all doable with pointer casts, but it's a bit easier, more self-documenting, and hence slightly safer to use a union in such a fashion.
In general though, if you're not sure if you need a union, you almost certainly do not need one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With