Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master the characteristics and application of Union in C language consortium

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to master the characteristics and application of Union of C language consortium". In daily operation, I believe many people have doubts about how to master the characteristics and application of Union of C language consortium. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to master the characteristics and application of Union of C language consortium". Next, please follow the editor to study!

I. definition of federation (common body)

Union is a special custom type, and the variable defined by this type also contains a series of members, characterized by the fact that these members share the same space, so the federation is also called a common body.

The code is as follows (example):

# declaration of the includeunion Un// union type, union is the federation keyword {char c, int, 4 bytes}; int main () {union Un u = {0}; printf ("% d\ n", sizeof (u)); printf ("% p\ n", & u); printf ("% p\ n", & (u.c)) / / u.c represents the member c of the consortium, and the reference method is similar to the structure printf ("% p\ n", & (u.i));}

The print result is as follows:

We know from sizeof (u) that the union occupies a total of 4 bytes, while the union member I is of type int, which takes up 4 bytes, the other c is the char type takes 1 byte, and the two occupy 4 bytes together. It shows that there must be one place where c and I share the same space, and the other is that u itself and its two members are the same address, as shown in the figure 003EFA80 above, indicating that the first address coincides with each other. The simple diagram is as follows:

As a result of this feature of shared space, you will change CMI as well. Here and the structure is completely different, the structure members are independent of each other, but the consortium is different, change one, the other will also change. So here, you can only use one consortium member at the same time, if you use c, don't use I, because when you change c, it will affect your use of I, and the program is very prone to problems.

Second, the characteristics and application of association.

Members of a federation share a piece of memory space, and the size of such a federated variable is at least the size of the largest member (because of the federation)

Let's take a look at the advantages of the consortium in determining whether memory storage is large-end storage or small-end storage.

The code is as follows (example):

Union U {char c; int i;} ubank / Union variable creation method analogous structure int main () {u.I = 1 / / 0x 00 00 00 01 / / low address-> High address / / 01 00 00 small end Storage low address / / 00 00 01 large end Storage low order High address if (u.c = = 1) {printf ("small end") } else {printf ("big end");}

Since I and c share a piece of memory, after we create I, we only need to determine whether 1 is at a high address or a low address, and because c and I are the same address (low), it is very convenient to determine whether there is a 1 or 0 in c.

Third, the calculation of the size of the union

Before calculating the size of the consortium, we must know two knowledge points:

1. The size of the union is at least the size of the largest member.

two。 When the maximum member size is not an integer multiple of the maximum alignment, it should be aligned to an integer multiple of the maximum alignment.

Examples are as follows:

# includeunion Un1 {char c [5]; / 1 char type occupies 1 byte, 5 takes 5 byte int iThanks 4 bytes}; union Un2 {short c [7]; / / 1 short type occupies 2 bytes, 7 occupies 14 bytes int iCompact 4 bytes}; int main () {printf ("% d\ n", sizeof (union Un1)) / / print 8 printf ("% d\ n", sizeof (union Un2)); / / print 16}

Un1 explained:

Char creates an array of size 5 in the same way as putting five char types. The alignment number is still 1.

The int type I itself has a size of 4 bytes, with a default alignment of 8 and an alignment of 4. The maximum alignment of I and c is 4, while the maximum member size is array c (5 bytes). 5 is not a multiple of 4. We need to align to an integral multiple of the maximum alignment, that is, 8 (3 bytes of space are wasted from 5 to 8)

Un2 explained:

As for the c array created by short, we know that the c alignment number is 2 and the maximum alignment number is 4. The maximum member size, that is, the c array size 14, is not an integral multiple of the maximum alignment number 4, 14 is an integral multiple of 4 when aligned up to 16.

At this point, the study on "how to master the characteristics and application of Union in C language consortium" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report