Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a union?

Tags:

c++

c

Recently I am working on windows and I found that lot of data structures are defined as struct with union as member variables. Example of this would be EVT_VARIANT in Windows.

I failed to understand what is the purpose behind this.

like image 759
Avinash Avatar asked Aug 26 '11 18:08

Avinash


People also ask

What is the purpose of an union?

Union members work together to negotiate and enforce a contract with management that guarantees the things you care about like decent raises, affordable health care, job security, and a stable schedule. Better workplaces and working conditions without the fear of retaliation.

What is a union and what is its role?

Unions play an important role in the workplace. Their key roles include acting as employee representatives during workplace disputes and acting as a bargaining representative during bargaining negotiations.

What does it mean to be in an union?

A union is an organization formed by workers who join together and use their strength to have a voice in their workplace. Through their union, workers have the ability to negotiate from a position of strength with employers over wages, benefits, workplace health and safety, job training and other work-related issues.

What is a union simple definition?

(yunyən ) Word forms: unions. countable noun. A union is a workers' organization which represents its members and which tries to improve things such as their working conditions and pay.


2 Answers

When a struct contains union members it's generally done as a space saving mechanism. If the struct can be of certain sub-types by which only certain members are valid then a union is a good way to not waste space.

For example

enum NumberKind {
  Integer, 
  FloatingPoint 
};

struct Number {
  NumberKind kind;
  union {
    int integerValue;
    float floatValue;
  };
};

In this scenario I've defined a struct Number which can have type types of numeric values: floating point and integer. It's not valid to have both at the same time so rather than waste space by having both members always defined I created a union which makes the storage of both equal to the size of the biggest.

Sample Usage of Above as requested

void PrintNumber(Number value) {
  if (value.kind == Integer) {
    printf("%d\n", value.integerValue);
  } else {
    printf("%f\n", value.floatValue);
  }
}
like image 159
JaredPar Avatar answered Sep 21 '22 00:09

JaredPar


Imagine you have a struct which holds a packet of data. The data in the packet can be of a few different types, so you store the type in a member called type. So to read the data in this packet, you first check the type, then you read the corresponding data member, which should contain the data in the packet.

Without unions the data struct would look like this:

struct data {
    type_t type;

    int number;
    char * string;
    double fraction;
    long long big_number;
}

This would be a rather big data structure. It uses enough room to store one of every possible data type. That's a little unnecessary when you will definitely only have one of those members containing useful information at any point in time.

If we use unions:

struct data {
    type_t type;

    union payload {
         int number;
         char * string;
         double fraction;
         long long big_number;
    }
}

Then the struct contains only enough space to store the type plus one of the payload members (i.e. you will have allocated an amount of memory equal to the size of type_t plus the size of the largest possible payload member). This saves a load of space and is much more efficient.

You do have to be careful however, because if the payload contains an int, you can still read it as a double. You will probably just get extremely strange numbers as your program tries to interpret the data wrongly

like image 33
Harry Eakins Avatar answered Sep 21 '22 00:09

Harry Eakins