当前位置:网站首页>Difference and connection between structure and Consortium
Difference and connection between structure and Consortium
2022-07-19 08:35:00 【Charmchin】
Define a structure as follows B And a consortium U :
// Defining structure
typedef struct{
int i;
double j;
}B;
// Define a consortium
typedef union{
int i;
double j;
}U;
Can pass sizeof() Function to check the memory size of structures and federations .sizeof(B) The value of is 12,sizeof(U) The value of is 8 instead of 12 . Why? sizeof(U) No 12 Well ? because union All members in share memory ,i and j The memory of is the same . And the overall memory size is divided by the members of the maximum memory , therefore U The memory size of is double Size .
therefore , We can also see that all members of the consortium share memory , And only one member can get the right to use this memory at the same time ( That is, read and write to memory ), Each member of the structure has its own memory , Use each other without interference .
Understand the concept of joint memory , You can see why you can only assign values to one member at a time , Because if you assign another value , Will overwrite the value of the previous member . All in all , The difference between the two can be summed up in the following two points .
- (1) Both structures and unions are made up of members of different data types , But at any moment , Only one selected member can be stored in the union , And all the members of the structure exist .
- (2) Assign values to different members of the consortium , Will be rewritten for other members , The value of the original member does not exist , The assignment to different members of a structure does not affect each other .
边栏推荐
猜你喜欢
随机推荐
深度学习之线性回归+基础优化
5.1 安全漏洞與防範
Redis6新数据类型——HyperLogLog
5.2 数据库安全
Address monitoring API: how to trace and monitor uniswap hacker addresses
Unity: window size adaptation when running on the browser after webgl Publishing
5.1 安全漏洞与防范
rosbridge
5.1 vulnérabilités et précautions en matière de sécurité
力扣1669合並兩個鏈錶筆記
Redis introduction
事件循环、宏任务、微任务
最新一代互联网:WEB 3.0
RestTemplate
5G正当时,无人驾驶未来将驶向何方?
Demo collection injection
凸面镜面3D玻璃轮廓扫描
Hand in hand practice a DAPP, the road to Web3.0!
Redis介绍
MySQL数据类型







![[kernel] character device that drives development and learning](/img/99/2eaed37078c3245be29d82382cfd59.png)

