问题
I am working on optimization of a project. It contains a struct of an options in which user can select a single option at a time. In addition to the option, we also use a flag variable to check which option value is set for this record. In order to make it memory efficient I want to convert struct into a union. But How do I know that which variable value is set in union. Because there is no restriction in union to fetch a value of a variable even which is not set.
struct options{
int basicPay;
int lumsumPay;
int mothlyPay;
int weeklyPay;
int dailyPay;
int anualPay;
int payType; // Flag variable to check which option is selected
};
union OptimizeOptions{
int basicPay;
int lumsumPay;
int mothlyPay;
int weeklyPay;
int dailyPay;
int anualPay;
int payType; // Confuse at here
};
回答1:
Have you tried a union
inside of a struct
? Let’s see the following example equivalent:
union options{
int basicPay;
int lumsumPay;
int mothlyPay;
int weeklyPay;
int dailyPay;
int anualPay;
};
struct record{
union options op; // Options union
int payType; // Flag variable to check which option is selected
}
The union (options
) will reserve memory for its largest variable and you can set its value and your structure (record) will keep track of that union memory block and the payType
flag value could be set which will tell your program to fetch the perticular variable of the union.
回答2:
Unions suffers from a problem that there is no easy way to know which member of a union was last changed. In order to keep track of this information, you can embed union
inside a structure that has one another member (called "tag field" or "discriminant"). The purpose of tag field is to remind which member is changed/updated. You can try this:
typedef struct{
int payType; // Tag field
union{
int basicPay;
int lumsumPay;
int mothlyPay;
int weeklyPay;
int dailyPay;
int anualPay;
}OptimizeOptions;
}Options;
But, there is no need to write six separate members for union in your case as all are of int
type. Therefore it can be reduced to
typedef struct{
enum{BASIC_PAY, LUMSUM_PAY, MONTHLU_PAY, WEEKLY_PAY, DAILY_PAY, ANNUAL_PAY} payType;
int pay;
}Options;
Lets understand the use of tag field with a simple example. Suppose we want an array which can store int
and double
type data. This would become possible by using union
. So, first define a union type which will store either int
or double
.
typedef union {
int i;
double d;
} Num;
Next we have to create an array whose elements are Num
type
Num num_arr[100];
Now, suppose we want to assign element 0
of the num_arr
to store 25
, while element 1
stores 3.147
. This can be done as
num_arr[0].i = 25;
num_arr[1].d = 3.147;
Now suppose that we have to write a function which will print the num_arr
elements. The function would be like this:
void print_num(Num n)
{
if(n contains integer)
printf("%d", n.i);
else
printf("%f", n.d);
}
Wait! How could print_num
will decide whether n
contains an integer or double
?
This will be done by using the tag field:
typedef struct{
enum{INT, DOUBLE} kind; //Tag field
union{
int i;
double d;
}u;
}Num;
So, each time a value is assigned to a member of u
, kind
must1 be set to either INT
or DOUBLE
to remind that what type we actually stored. For example:
n.u.i = 100;
n.kind = INT;
The print_num
function would be like this:
void print_num(Num n)
{
if(n.kind == INT)
printf("%d", n.i);
else
printf("%f", n.d);
}
1: It is programmer's responsibility to update the tag field with each assignment to the member of union
. Forgetting to do so will lead to bug, as pointed in comment by @ j_random_hacker.
来源:https://stackoverflow.com/questions/26297188/how-to-know-which-variable-value-is-set-for-union