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
You use a union when your "thing" can be one of many different things but only one at a time.
You use a structure when your "thing" should be a group of other things.
For example, a union can be used for representing widgets or gizmos (with a field allowing us to know what it is), something like:
struct {
int isAGizmo;
union {
widget w;
gizmo g;
}
}
In this example, w
and g
will overlap in memory.
I've seen this used in compilers where a token can be a numeric constant, keyword, variable name, string, and many other lexical elements (but, of course, each token is one of those things, you cannot have a single token that is both a variable name and a numeric constant).
Alternately, it may be illegal for you to process gizmos without widgets, in which case you could use:
struct {
widget w;
gizmo g;
}
In this case, g
would be at a distinct memory location, somewhere after w
(no overlap).
Use cases for this abound, such as structures containing record layouts for your phone book application which will no doubt earn you gazillions of dollars from your preferred app store :-)
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.
Ins some cases we may have to use only one variable at a time but the different results will have to be stored with different names. In such cases union will help by allocating same space for each variable at one location with the maximum variable size.
i.e if we use int and char then union will allocate space for char which has the bigger size and the int too 'll be stored in the same space overriding the last one.
You cannot compare unions to structures, it's like comparing apples to oranges, they are used for different things. Unions are typically used in situations where space is premium but more importantly for exclusively alternate data. Unions help eliminate typos and ensure that mutually exclusive states remain mutually exclusive because error in programming logic will surface more quickly when we use unions for mutually exclusive data.
Also unions can lead to much easier pointer mathematics when working with complex data passing between components. case-in-point when developing compilers with LEX and YACC the values are passed from the lexer to the parser in a union. The parser implementation and subsequent typecasting is made significantly easier because of the use of union.