当前位置:网站首页>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 .
边栏推荐
猜你喜欢

uCOS-III 的特点、任务状态、启动
![[yarn] yarn container log cleaning](/img/1d/b48059ae2e6133ea9d9c38d31c4a86.png)
[yarn] yarn container log cleaning

JS object and event learning notes

分布式事务的实现方案

FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现

5G工作原理详解(解释&图解)

RT-Thread的main线程“卡死”的一种可能原因及解决方案

电商数据分析--薪资预测(线性回归)
![[yarn] CDP cluster yarn configuration capacity scheduler batch allocation](/img/85/0121478f8fc427d1200c5f060d5255.png)
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation

Unit test - unittest framework
随机推荐
分布式事务的实现方案
4、安装部署Spark(Spark on Yarn模式)
[NPUCTF2020]ReadlezPHP
高通&MTK&麒麟 手机平台USB3.0方案对比
Nodejs connect mysql
OPPO VOOC快充电路和协议
E-commerce data analysis -- User Behavior Analysis
arduino获取数组的长度
2020网鼎杯_朱雀组_Web_nmap
Hutool中那些常用的工具类和方法
[Bluebridge cup 2021 preliminary] weight weighing
2019腾讯暑期实习生正式笔试
PyTorch四种常用优化器测试
RT-Thread API参考手册
inline详细讲解【C语言】
Redis interview questions
Those commonly used tool classes and methods in hutool
[yarn] yarn container log cleaning
encoderMapReduce 随手记
TypeScript