当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
express框架详解
C语言函数之可变参数原理:va_start、va_arg及va_end
List and set
[BSidesCF_2020]Had_a_bad_day
[MRCTF2020]套娃
【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
常用正则表达式整理
使用LinkedHashMap实现一个LRU算法的缓存
Wangeditor rich text reference and table usage
SQL time injection
【flink】flink学习
ESP8266通过arduino IED连接巴法云(TCP创客云)
[Flink] cdh/cdp Flink on Yan log configuration
uCOS-III 的特点、任务状态、启动
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
冒泡排序【C语言】
Machine learning -- decision tree (sklearn)
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Raspberry pie tap switch button to use
Using LinkedHashMap to realize the caching of an LRU algorithm