C - What is Union? Union and Struct are so similar, what's the difference? Why create a union? Where do I need to use it?

In the C language, there is a unionvariable 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 structsame as that of union, as follows:

union u_tag {
    
    
    int ival;
    char *sval;
} u;

As you can see, the only difference is structthe 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, structwhich is unioncompletely different from the purpose of use. unionIt does not seem structto 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 unionit will not be based on the previous u.a=1output like a structure 1. Since only one type of data can be stored, the latter u.b=2will overwrite the originally ustored value.

Therefore, unioninternally declared variables can be understood as a possibility, not actual objects or member variables .

And since unionthere 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 structit unionis different from the core, and unionthe 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_tagthe size of is 8 bytes, but s_tagthe size of is 16 bytes. This is because it unionis equivalent to leaving a maximum space for the internally declared data type, structbut leaving its own space for each data type.

On most machines today, intthe type is 4 bytes, floatthe type is 4 bytes, and cahr *the type is 8 bytes. So uit occupies the largest space, which is 8 bytes, and seach 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, intaddresses 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~

Guess you like

Origin blog.csdn.net/qq_33919450/article/details/130613405