In the C language, there is a union
variable called a variable, which is used to store objects of different types and sizes in different situations. This is very similar to a struct struct
: a struct is a collection of one or more variables.
The declaration method of union is very simple and exactly the struct
same as that of union, as follows:
union u_tag {
int ival;
char *sval;
} u;
As you can see, the only difference is struct
the change in the structure union
.
The method of use is exactly the same:
//一般变量
union_name.val
//指针
union_pointer->val
The two are so similar, so what is the difference between the two? In other words, why do you want to get two such similar things? C is not C++, some functions are developed by different groups, and some functions will overlap.
The first is the superficial difference, struct
which is union
completely different from the purpose of use. union
It does not seem struct
to store every variable listed in the braces at the same time. If you try the following code
union u_tag {
int a;
int b;
} u;
int main()
{
u.a=1;
u.b=2;
printf("u_tag: %d\n", u.a);
return 0;
}
Then the output result is:
u_tag: 2
The result here is not wrong, because union
it will not be based on the previous u.a=1
output like a structure 1
. Since only one type of data can be stored, the latter u.b=2
will overwrite the originally u
stored value.
Therefore, union
internally declared variables can be understood as a possibility, not actual objects or member variables .
And since union
there is only one value inside, the same data type is generally not used. As for what data type is currently included, this is what the programmer needs to pay attention to when programming . Generally, a variable is used u_type
, and then a judgment statement is used to judge the type currently contained.
But struct
it union
is different from the core, and union
the reason for its birth is: the mechanism of allocating space.
If you sizeof()
measure the size of the following two data with
union u_tag {
int ival;
float fval;
char *sval;
} u;
struct s_tag {
int ival;
float fval;
char *sval;
} s;
You will find that u_tag
the size of is 8 bytes, but s_tag
the size of is 16 bytes. This is because it union
is equivalent to leaving a maximum space for the internally declared data type, struct
but leaving its own space for each data type.
On most machines today, int
the type is 4 bytes, float
the type is 4 bytes, and cahr *
the type is 8 bytes. So u
it occupies the largest space, which is 8 bytes, and s
each has its own space, which is 4+4+8=16 bytes.
So when do you need to use it union
?
First of all, according to its characteristics of storing a kind of data, it can be used to do some general calculations, such as switching between floating point numbers and integers.
Secondly, under the above purpose of use, it saves space. Although structures can also be used in this case, the situation where the above three data types are only twice the size is considered good. Because of the memory address allocation mechanism, the addresses of some types of data are specially required. For example, on some machines, int
addresses in even digits are required.
The following is an example, but the integer address limit is not even, but a multiple of 4.
struct s_tag {
int ival;
char a;
} s;
The size of this structure is 8 bytes, you can try it yourself to see if it is this size.
And since the compiler reads sequentially, if you write
struct s_tag {
int a;
char b;
int c;
char d;
} s;
Then the size of this structure is 16 bytes. In this case, data of the same type can be written together to save some space, as follows:
struct s_tag {
int a;
int c;
char b;
char d;
} s;
In this way, the space occupied by this structure is 12 bytes. And if used union
, the size is always 4 bytes regardless of the order. As the possibilities for data types increase, the need to save space becomes apparent.
Hope to help those in need~