When to use a union and when to use a structure

20,905

Solution 1

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.

Solution 2

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 :-)

Share:
20,905
karan
Author by

karan

Updated on December 30, 2020

Comments

  • karan
    karan over 3 years

    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?