当前位置:网站首页>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 .
边栏推荐
- 【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
- TypeScript
- Redis面试题
- C语言回调函数【C语言】
- Those commonly used tool classes and methods in hutool
- 机器学习--决策树(sklearn)
- L2-007 family real estate (25 points)
- Contiki source code + principle + function + programming + transplantation + drive + network (turn)
- PyTorch四种常用优化器测试
- Using LinkedHashMap to realize the caching of an LRU algorithm
猜你喜欢
随机推荐
5G工作原理详解(解释&图解)
R & D thinking 01 ----- classic of embedded intelligent product development process
Mall project -- day09 -- order module
L2-001 emergency rescue (25 points)
ESP8266通过arduino IED连接巴法云(TCP创客云)
MongoDB
【flink】flink学习
高通&MTK&麒麟 手机平台USB3.0方案对比
使用LinkedHashMap实现一个LRU算法的缓存
Word排版(小计)
树莓派 轻触开关 按键使用
Wangeditor rich text reference and table usage
C语言回调函数【C语言】
[Kerberos] deeply understand the Kerberos ticket life cycle
Reading notes of difficult career creation
OPPO VOOC快充电路和协议
4. Install and deploy spark (spark on Yan mode)
互联网协议详解
Mysql的索引实现之B树和B+树
【yarn】CDP集群 Yarn配置capacity调度器批量分配