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.
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.
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.
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.
(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.
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);
}
}
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
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