当前位置:网站首页>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 .
边栏推荐
- nodejs连接Mysql
- error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
- MongoDB
- sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
- 优先级反转与死锁
- Contiki source code + principle + function + programming + transplantation + drive + network (turn)
- {one week summary} take you into the ocean of JS knowledge
- [mrctf2020] dolls
- yarn安装与使用
- 锂电池基础知识
猜你喜欢
高通&MTK&麒麟 手機平臺USB3.0方案對比
Password free login of distributed nodes
RT-Thread API参考手册
mysql实现读写分离
Apprentissage automatique - - régression linéaire (sklearn)
B tree and b+ tree of MySQL index implementation
电商数据分析--用户行为分析
[template] KMP string matching
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
MySQL数据库面试题
随机推荐
Redis interview questions
ESP8266使用arduino连接阿里云物联网
Unit test - unittest framework
2020网鼎杯_朱雀组_Web_nmap
分布式節點免密登錄
Encodermappreduce notes
Wangeditor rich text component - copy available
互联网协议详解
R & D thinking 01 ----- classic of embedded intelligent product development process
Stage 4 MySQL database
Raspberry pie tap switch button to use
Mall project -- day09 -- order module
[mrctf2020] dolls
[CDH] modify the default port 7180 of cloudera manager in cdh/cdp environment
MongoDB
MATLAB学习和实战 随手记
RT-Thread的main线程“卡死”的一种可能原因及解决方案
Small L's test paper
[Blue Bridge Cup 2017 preliminary] grid division
inline详细讲解【C语言】