当前位置:网站首页>Detailed explanation of Union [C language]
Detailed explanation of Union [C language]
2022-07-06 11:58:00 【Weiyuan escort agency】
We know the structure (Struct) Is a construction type or complex type , It can contain multiple members of different types . stay C In language , There's another syntax that's very similar to the structure , be called Shared body (Union), It is defined in the form of :
union Community name {
Member list
};
Commons are sometimes called unions or consortia , This is also Union The original meaning of this word .
Structure and common body difference lie in : Each member of the structure takes up different memory , There is no influence on each other ; And all members of the Commons occupy the same segment of memory , Modifying one member affects all other members .
The memory occupied by the structure is greater than or equal to the total memory occupied by all members ( There may be gaps between members ), The memory occupied by the Commons is equal to the memory occupied by the longest member . Commons use memory overlay technology , Only one member's value can be saved at the same time , If you assign a value to a new member , It will override the value of the original member .
Commons are also a custom type , You can use it to create variables , for example :
union data{
int n;
char ch;
double f;
};
union data a, b, c;
The above is to define the common body first , Then create variables , You can also create variables while defining a common body :
union data{
int n;
char ch;
double f;
} a, b, c;
If you don't define new variables anymore , You can also omit the name of the common body :
union{
int n;
char ch;
double f;
} a, b, c;
Shared body data in , member f Takes up the most memory , by 8 Bytes , therefore data Variable of type ( That is to say a、b、c) It also takes up 8 Bytes of memory , Please see the following Demo :
#include <stdio.h>
union data{
int n;
char ch;
short m;
};
int main(){
union data a;
printf("%d, %d\n", sizeof(a), sizeof(union data) );
a.n = 0x40;
printf("%X, %c, %hX\n", a.n, a.ch, a.m);
a.ch = '9';
printf("%X, %c, %hX\n", a.n, a.ch, a.m);
a.m = 0x2059;
printf("%X, %c, %hX\n", a.n, a.ch, a.m);
a.n = 0x3E25AD54;
printf("%X, %c, %hX\n", a.n, a.ch, a.m);
return 0;
}
Running results :
4, 4
40, @, 40
39, 9, 39
2059, Y, 2059
3E25AD54, T, AD54
This code not only verifies the length of the common body , It also shows that members of the community will interact with each other , Changing the value of one member will affect other members .
To understand the output above , Find out how members interact , You have to understand the distribution of each member in memory . On the surface of the above data For example , The distribution of each member in memory is as follows :
member n、ch、m In memory “ alignment ” To the end , Yes ch The assignment modifies the previous byte , Yes m The assignment changes the first two bytes , Yes n The assignment modifies all bytes . in other words ,ch、m Will affect n Part of the data , and n Will affect ch、m Full data .
The picture above is in the vast majority of PC Memory distribution on the computer , If it is 51 Single chip microcomputer , Things will be different :
Why do different machines have different distributions ? This is related to the storage mode of the machine , Big end and small end .
边栏推荐
- MySQL主从复制的原理以及实现
- Contiki source code + principle + function + programming + transplantation + drive + network (turn)
- [Bluebridge cup 2020 preliminary] horizontal segmentation
- 【yarn】CDP集群 Yarn配置capacity调度器批量分配
- 【yarn】Yarn container 日志清理
- Common regular expression collation
- Wangeditor rich text reference and table usage
- uCOS-III 的特点、任务状态、启动
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
- JS object and event learning notes
猜你喜欢
随机推荐
[BSidesCF_2020]Had_ a_ bad_ day
nodejs连接Mysql
Nodejs connect mysql
Machine learning -- decision tree (sklearn)
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
[template] KMP string matching
express框架详解
[CDH] modify the default port 7180 of cloudera manager in cdh/cdp environment
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
RT-Thread API参考手册
[Bluebridge cup 2021 preliminary] weight weighing
Pytorch-温度预测
Linux yum安装MySQL
荣耀Magic 3Pro 充电架构分析
关键字 inline (内联函数)用法解析【C语言】
Togglebutton realizes the effect of switching lights
Stage 4 MySQL database
Encodermappreduce notes
MySQL主从复制的原理以及实现
Connexion sans mot de passe du noeud distribué